![]() |
VOOZH | about |
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 elements 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 a lesser ASCII value will be placed first than the character with a higher ASCII value.
In Python, sorting any sequence is very easy as it provides in-built methods for sorting. Two such methods are sorted() and sort(). These two methods are used for sorting but are quite different in their way. Let's have a look at them one by one.
sorted() method sorts the given sequence as well as set and dictionary(which is not a sequence) either in ascending order or in descending order(does Unicode comparison for string char by char) and always returns a sorted list. This method doesn't affect the original sequence.
Syntax: sorted(iterable, key, reverse=False)
sort() in Python function is very similar to sorted() but unlike sorted it returns nothing and makes changes to the original sequence. Moreover, sort() in Python is a method of list class and can only be used with lists.
Syntax: List_name.sort(key, reverse=False)
Here, we are discussing Difference between sorted() and sort() on below points with the help of the code examples.
sorted() Method: In this example, below code uses the sorted list by arranging its elements in order. After that, it prints the original list to prove that the order of the elements in the original list hasn't changed.
Output:
Sorted list: [1, 2, 3, 4, 5] Original list after sorting: [1, 5, 4, 2, 3]
sort() Method: In this example , below code takes a list of numbers and sorts it directly, changing the order of the elements in the original list. It then prints the sorted list .
Output:
Sorted list: [1, 2, 3, 4, 5] Original list after sorting: [1, 2, 3, 4, 5]
sorted() Method: In this example , below code shows use of the `sorted()` function with different data types. First, it sorts a list of characters alphabetically, then it sorts a tuple, sorts the characters in a string based on their ASCII values, providing an ordered representation for each data type.
Output:
Original List: ['q', 'w', 'r', 'e', 't', 'y']
Sorted List: ['e', 'q', 'r', 't', 'w', 'y']
Original Tuple: ('q', 'w', 'e', 'r', 't', 'y')
Sorted Tuple: ['e', 'q', 'r', 't', 'w', 'y']
sort() Method: In this example, below code sorts a list of characters in-place using the `sort()` method, and then, for a tuple and a string, it converts them to lists, sorts, and prints the elements in ascending order, demonstrating the modification of the original order in each case.
Output :
Original List: ['q', 'w', 'r', 'e', 't', 'y']
Sorted List: ['e', 'q', 'r', 't', 'w', 'y']
Original Tuple: ('q', 'w', 'e', 'r', 't', 'y')
Sorted Tuple: ['e', 'q', 'r', 't', 'w', 'y']In this example Python code demonstrates the use of the `sort()` method to sort lists of integers, floating-point numbers, and strings in descending order. Here, the lists (`numbers`, `decimalnumber`, and `words`) are sorted in reverse, and the sorted results are printed to showcase descending sorting for each data type.
Output:
Original List: [1, 3, 4, 2]
Sorted List (Descending Order): [4, 3, 2, 1]
Original List: [2.01, 2.0, 3.67, 3.28, 1.68]
Sorted List (Descending Order): [3.67, 3.28, 2.01, 2.0, 1.68]
Original List: ['Geeks', 'For', 'Geeks']
Sorted List (Descending Order): ['Geeks', 'Geeks', 'For']
In this example Python code demonstrates the use of the `sorted()` method to sort lists of integers, floating-point numbers, and strings in descending order. Here, the lists (`numbers`, `decimalnumber`, and `words`) are sorted in reverse, and the sorted results are printed to showcase descending sorting for each data type.
Output :
Original List: [1, 3, 4, 2]
Sorted List (Descending Order): [4, 3, 2, 1]
Original List: [2.01, 2.0, 3.67, 3.28, 1.68]
Sorted List (Descending Order): [3.67, 3.28, 2.01, 2.0, 1.68]
Original List: ['Geeks', 'For', 'Geeks']
Sorted List (Descending Order): ['Geeks', 'Geeks', 'For']
Related Article:
Let us see the differences in a tabular form:
| sorted() | sort() |
|---|---|
| The sorted() function returns a sorted list of the specific iterable object. | The sort() method sorts the list. |
| We can specify ascending or descending order while using the sorted() function | It sorts the list in ascending order by default. |
Its Syntax is : sorted(iterable, key=key, reverse=reverse) | Its Syntax is : list.sort(reverse=True|False, key=myFunc) |
| Its return type is a sorted list. | We can also use it for sorting a list in descending order. |
Does not modify the original iterable | Modifies the original list in-place |
Accepts additional parameters such as | Accepts |
| It can only sort a list that contains only one type of value. | It sorts the list in-place. |
Can handle different types of iterables | Limited to lists, raises an error for other types |
Compatible with any iterable | Only applicable to lists |
Can be used with strings for alphabetical sorting | Directly applicable to lists of strings |