The sort function can be used to sort a list in ascending, descending or user defined order.
To sort the list in ascending order.
This will sort the given list in ascending order.
This function can be used to sort list of integers, floating point number, string and others.
[1, 2, 3, 4]
To sort the list in descending order.
This will sort the given list in descending order.
[4, 3, 2, 1]
Sorting user using user defined order.
list_name.sort(key=…, reverse=…) – it sorts according to user’s choice
[(1, 1), (1, 2), (3, 3)] [(3, 3), (1, 2), (1, 1)]
- Python | Sort list of list by specified index
- Python | Sort the values of first list using second list
- Python | Sort list according to other list order
- Python | How to sort a list of strings
- Python | Sort list of dates given as strings
- Python | Sort a List according to the Length of the Elements
- Python | Sort a list according to the second element in sublist
- Python | Sort numeric strings in a list
- Python program to sort a list of tuples by second Item
- List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
- Ways to sort list of dictionaries by values in Python – Using itemgetter
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Sort a linked list of 0s, 1s and 2s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.