What Is Sorting?
In the programming world, sometimes you may have a problem with unordered items in a list where you would need them to be in order. The solution for this lies in Sorting. Sort in Python is nothing but arranging the data in a particular format or order. The sorting can be both ascending and descending of data elements. Like C++ and Java, Python programming also has some built-in functions, which help you sort the elements in the list.
What list.sort() in Python?
List.sort() is a built-in function in Python. This function is defined for lists and used to modify the order of data in the list. The list that contains ‘Integers’, ‘Floating Point Numbers’, ‘String’, or others can be sorted using list.sort(), which is a built-in sort in Python.
Syntax: List_name.sort()
Example:
Output:
In the example above, you saw the list.sort() do the sorting function in ascending order. Let’s see how to sort when it comes to descending order.
Syntax: list_name.sort(reverse=True)
Example:
Output:
What is Sorted() in Python?
Sorted() is a built-in Python method, which returns the sorted list of old lists.
The return type for the sorted() method is a list.
Syntax:
Var_newList = sorted(old list)
Example:
Output:
The Difference Between sort() and sorted()
Sort() |
Sorted() |
Sorts the array in place and returns none. |
It returns a new sorted list from the old list. |
It does the direct sorting on the existing list. |
It creates a copy of the original list and sorts the copied list. |
Consumes less space since the sorting happens in the existing list. |
Consumes more space as the sorting happens in the copied old list. |
Faster |
Slower |
The return type is none |
The return type is a list |
Parameters in Sort:
The sort() method does not require any parameter to be passed by default, but there are a couple of optional parameters supported. They are listed below with some examples of the same.
- reverse
- key
i) Reverse: As the name implies, it reverses the order. If it sets the reverse to be true, it will sort the list in descending order. Else if it sets the reverse to false, then it will sort the list in ascending order.
Example 1: reverse = True
Output:
Example 2: reverse = False
Output:
ii) Key: It is the function that acts as a key for sort comparison, which means that the key is nothing but its own sorting implementation or sorting as per the user’s choice. Sorting the user uses the user-defined order.
Example:
Output:
Operator Module Functions:
Operator module functions are nothing but convenient functions that are provided by Python to make the function access easier and faster.
The Operator module has the following three functions. They are:
- a) itemgetter
- b) attrgetter
- c) methodcaller
A) Itemgetter Operator Module Function Example.
Output:
B) Attrgetter Operator Module Function Example.
Output:
C) The Third and Last Function Methodcaller From Operator Module Functions.
Ascending and Descending Sort in Python()
The two built-in Python sort functions are list.sort() and sorted(). Both these have an optional parameter ‘reverse’ that accepts a Boolean value. Boolean value ‘true’ does the descending order and Boolean value ‘false’ does the ascending order. It is to be noted that by default, sort and list.sort() are in ascending orders.
Since you have seen list.sort() with reverse parameters already in the previous sections, go through ascending and descending examples for the sorted() function.
Example 1: Sorted Ascending Using reverse
Output:
Example 2: Sorted Descending Using reverse
Output:
Python sorted functionality is stable or guaranteed to be stable. This is helpful for sorting through multiple passes.
Find Our Python Training in Top Cities |
Sort Stability and Complex Sorts:
Stable Sort:
The stable sorting is guaranteed from Python version 2.2. For multiple records that have the same key for specifying the order of sorting, their original order is preserved. So, when the stable sort is over one item and has the same key, their order will be preserved.
Check out this quick example to understand how it works.
from operator import itemgetter
eatery_data = [('vegetarian', 1), ('eggetarian', 2), ('vegetarian', 2)]
print(sorted(eatery_data, key=itemgetter(0)))
Example
Output:
You can notice from the output of the above program that the vegetarian records preserved their original order as (‘vegetarian’, 1) guaranteed, then (‘vegetarian’, 2).
Complex Sort:
It’s very helpful to build complex sorts in a series of sorting steps.
See this quick example. Start the candidate data by descending age, then by the ascending grade, but grade sort first, and then by the grade age.
Example:
Output:

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer
Conclusion:
The sorting is meant by ordering in descending or ascending array of elements. This is one of the key functions in Python programming.
As mentioned, Python has built-in sort functions that are used to sort either ascending or descending data elements from the list of integers, list of floating-point numbers, list of strings, and others.
In this article, you have learned what is Python list sort, what is list.sort() and sorted() functions, what is the difference between sort and sorted functions, what are the different parameters supported for sort in Python, what are operator module functions in Python, different operator module functions, sort in Python for descending, ascending orders, sort stability and what it does. You also looked into complex sorts in Python, how sort stability of Python helps complex sorts in Python and required working examples for you to play around with.
To learn more about iterators in Python, join Simplilearn’s Data Science with Python Certification Course. The Python Data Science course will show you how to learn Python programming concepts. You can gain expertise in data mining, machine learning, data visualization, web crawling, and natural language processing. You will master the basic tools of Data Science with Python after completing this course.
This course includes 68 hours of blended learning, four business-based assignments, interactive learning with Jupyter notebooks laboratories, lifelong self-paced learning, and dedicated mentoring sessions from industry experts.
If you have any questions for us, leave them in the comments section of this tutorial. Our experts will get back to you on the same, at the earliest. Happy learning!