In this section, we will learn what the sorted() function is and how to use it in Python.
sorted() Function in Python
The Python sorted() function is used to sort the elements of an iterable object like a List, Tuple, Set, Dictionary, etc.
Note that this function does not modify the original iterable object (the one that the sorting operation is taking upon) but instead the function returns a new list object with the elements of the target iterable object all sorted out.
Python sorted() Function Syntax:
sorted(iterable, key=key, reverse=reverse)
Python sorted() Function Parameters
The function takes 3 arguments:
- The first argument is the target iterable object that we want to create a sorted list using the elements of that iterable object.
- The second element is a reference to a function. We use this function when the elements of the target iterable object are of complicated data type (like a dictionary for example) and the `sorted()` function doesn’t know based on what value it should sort the elements. Basically, the sorted() function only is able to sort elements of basic types like string and number. Now, the sorted() function use the `key` function and pass every element to that iterable object and expect to get an element of a simple data type and based on the results of this `key` function, it now knows how to sort the elements. Note: this second argument is optional.
- The last argument is a boolean value which defines whether the order of elements should be in ascending or descending mode. The value False means order the elements in ascending mode and the value True means descend the elements. The default value is set to False (ascending order).
Python sorted() Function Return Value
The return value of this function is a list object with the elements of the target iterable object all ordered.
Example: python sorted list
list1 = [124,56,32,675,64332,32424,5634,353] result = sorted(list1) print(list1) print(result)
Output:
[124, 56, 32, 675, 64332, 32424, 5634, 353] [32, 56, 124, 353, 675, 5634, 32424, 64332]
Example: sorted an array in python
list1 = ["Jack","Omid","Elon","Ellen","James"] result = sorted(list1, reverse = True) print(list1) print(result)
Output:
['Jack', 'Omid', 'Elon', 'Ellen', 'James'] ['Omid', 'James', 'Jack', 'Elon', 'Ellen']
Example: sorted dictionary in python
list1 = [ {"name":"Omid","age":29}, {"name":"Jack","age":50}, {"name":"Elon","age":12}, {"name":"Bill","age":10} ] def func(element): return element["age"] result = sorted(list1, key = func,reverse = False) print(list1) print(result)
Output:
[{'name': 'Omid', 'age': 29}, {'name': 'Jack', 'age': 50}, {'name': 'Elon', 'age': 12}, {'name': 'Bill', 'age': 10}] [{'name': 'Bill', 'age': 10}, {'name': 'Elon', 'age': 12}, {'name': 'Omid', 'age': 29}, {'name': 'Jack', 'age': 50}]
Example: python sorted reverse
list1 = [ {"name":"Omid","age":29}, {"name":"Jack","age":50}, {"name":"Elon","age":12}, {"name":"Bill","age":10} ] def func(element): return element["age"] result = sorted(list1, key = func,reverse = True) print(list1) print(result)
Output:
[{'name': 'Omid', 'age': 29}, {'name': 'Jack', 'age': 50}, {'name': 'Elon', 'age': 12}, {'name': 'Bill', 'age': 10}] [{'name': 'Bill', 'age': 10}, {'name': 'Elon', 'age': 12}, {'name': 'Omid', 'age': 29}, {'name': 'Jack', 'age': 50}]
Example: Sorting a String in Python
strValue = "my name is omid dehgahn" res = sorted(strValue) strValue = "" for character in res: strValue +=character print(strValue)
Output:
aaddeeghhiimmmnnosy
Example: sort list alphabetically python
list1 = ["Omid","Jack","James","John"] res = sorted(list1) print(res)
Output:
['Jack', 'James', 'John', 'Omid']