Sorting means rearranging a given sequence of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of the element in the respective data structure.
For example: The below list of characters is sorted in increasing order of their ASCII values. That is, the character with lesser ASCII value will be placed first than the character with higher ASCII value.
In Python, sorting any sequence is very easy as it provides in-built methods for sorting. Two such methods are
sort(). These two methods are used for sorting but are quite different in their own way. Let’s have a look at them one by one.
sorted() method sorts the given sequence either in ascending order or in descending order and always return the a sorted list. This method doesnot effect the original sequence.
Syntax: sorted(iterable, key, reverse=False)
Iterable: sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
Key(optional): A function that would serve as a key or a basis of sort comparison.
Reverse(optional): If set True, then the iterable would be sorted in reverse (descending) order, by default it is set as False.
Return Type: Returns a sorted list.
Sorted list: [1, 2, 3, 4, 5] Reverse sorted list: [5, 4, 3, 2, 1] Original list after sorting: [1, 2, 3, 4, 5]
Example 2: Sorting different data types
['e', 'q', 'r', 't', 'w', 'y'] ['e', 'q', 'r', 't', 'w', 'y'] ['h', 'n', 'o', 'p', 't', 'y'] ['e', 'q', 'r', 't', 'w', 'y'] ['e', 'q', 'r', 't', 'w', 'y']
Using key parameter
This optional parameter key takes a function as it’s value. This key function transforms each element before sorting, it takes the value and returns 1 value which is then used within sort instead of the original value.
Example: Let’s suppose we want to sort a List of string according to its length. This can be done by passing the
len() function as the value to the key parameter. Below is the implementation.
['aaaa', 'bbb', 'cc', 'd'] ['d', 'cc', 'bbb', 'aaaa']
sort() function is very similar to sorted() but unlike sorted it returns nothing and makes changes to the original sequence. Moreover, sort() is a method of list class and can only be used with lists.
Syntax: List_name.sort(key, reverse=False)
key: A function that serves as a key for the sort comparison.
reverse: If true, the list is sorted in descending order.
Return type: None
[1, 2, 3, 4] [1.68, 2.0, 2.01, 3.28, 3.67] ['For', 'Geeks', 'Geeks']
Example 2: Sorting in reverse order.
[4, 3, 2, 1] [3.67, 3.28, 2.01, 2.0, 1.68] ['Geeks', 'Geeks', 'For']
Example 3: Using key parameter.
[(1, 1), (1, 2), (3, 3)] [(3, 3), (1, 2), (1, 1)]
- a.sort(), sorted(a), np.argsort(a) and np.lexsort(b, a) in Python
- Python - Sort Dictionary by Value Difference
- Python | Sort tuple list on basis of difference of elements
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Sorted() function in Python
- Python | Combining two sorted lists
- Python | Check if list is sorted or not
- Python sorted containers | An Introduction
- Python - Sorted Nested Keys in Dictionary
- Python | Get items in sorted order from given dictionary
- Python | Relative sorted order in Matrix
- Merge two sorted arrays in Python using heapq
- Python | Check for Descending Sorted List
- Python | Returning index of a sorted list
- Python sorted() to check if two strings are anagram or not
- Python program to insert an element into sorted list
- Python - Sorted order Dictionary items pairing
- sort() in Python
- Python | Sort Python Dictionaries by Key or Value
- Python | Inserting item in sorted list maintaining order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.