In Python, the sorted() function is a built-in function for sorting iterable objects. It can return a new sorted list without modifying the original list. The sorted() function has a number of useful parameters that can be used to control the behavior of sorting. Below we will take a closer look at the usage of the sorted() function in Python.
1.Basic usage
python
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_numbers = sorted(numbers)
print(sorted numbers) output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9].
In this example, we define a list of integers and sort them using the sorted() function. The sorted() function returns a new list of sorted elements.
2.Sort order
By default, the sorted() function sorts iterable objects in ascending order. If you want to sort in descending order, you can use the reverse parameter.
python
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_numbers = sorted(numbers, reverse=true)
print(sorted numbers) output: [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1].
In this example, we set the reverse parameter to true to make the sorted() function sort the list in descending order.
3.Compare functions
By default, the sorted() function uses the less than operator to compare elements in an iterable object. However, you can pass a comparison function to specify a custom comparison logic. The comparison function should take two arguments and return a Boolean value indicating whether the first argument is less than the second argument.
For example, let's say we want to sort a list of strings by length, instead of the default dictionary order. We can use the len() function as a comparison function to achieve this.
python
words = ['apple', 'banana', 'cherry', 'date']
sorted_words = sorted(words, key=len)
print(sorted words) output: [.]'date', 'apple', 'cherry', 'banana']
In this example, we use the len() function as the value of the key parameter, specifying that the order is sorted by the length of the string. This will cause the sorted() function to use the return value of the len() function when comparing elements, instead of the default dictionary order comparison.
4.Stable sorting
By default, the sorted() function uses a stable sorting algorithm, which means that when two elements are equal, their position after sorting is consistent with the original position. This means that a stable sorting algorithm can process lists with duplicate elements and ensure that equal elements remain in relative order. This can be useful in some cases, such as when working with a list of student grades that contain duplicate scores.
Note that when using a comparison function, you must ensure that the return value of the comparison function is stable for equal elements in order to maintain the stable sorting property. If the comparison function returns an unstable result for equal elements, then the sort result will no longer be stable.
The sorted() function is a very useful feature in Python to help us sort iterable objects conveniently. By understanding its usage and parameters, we can better control the behavior of the sorting to meet specific needs.