Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

sort() in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The sort function can be used to sort the list in both ascending and descending order. It can be used to sort lists of integers, floating point numbers, strings, and others. Its time complexity is O(NlogN).

Python sort() Syntax

The syntax of the sort() function in Python is as follows.

Syntax: list_name.sort(key=…, reverse=…)

Parameters:

By default, Python sort() doesn’t require any extra parameters and sorts the list in ascending order. However, it has two optional parameters:

  • key:  function that serves as a key for the sort comparison
  • reverse: If true, the list is sorted in descending order.

Return value: The sort() does not return anything but alters the original list according to the passed parameter.

sort() in Python Examples

Sorting List in Ascending Order

By default, the sort() in Python sorts a list in ascending order if we do not provide it with any parameters.

Python3




# List of Integers
numbers = [1, 3, 4, 2]
 
# Sorting list of Integers
numbers.sort()
 
print(numbers)
 
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
 
# Sorting list of Floating point numbers
decimalnumber.sort()
 
print(decimalnumber)
 
# List of strings
words = ["Geeks", "For", "Geeks"]
 
# Sorting list of strings
words.sort()
 
print(words)


Output:

[1, 2, 3, 4]
[1.68, 2.0, 2.01, 3.28, 3.67]
['For', 'Geeks', 'Geeks']

Sorting List in Descending Order

To sort a list in descending order, set the reverse parameter to True of the sort() function in Python.

Python3




# List of Integers
numbers = [1, 3, 4, 2]
 
# Sorting list of Integers
numbers.sort(reverse=True)
 
print(numbers)
 
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
 
# Sorting list of Floating point numbers
decimalnumber.sort(reverse=True)
 
print(decimalnumber)
 
# List of strings
words = ["Geeks", "For", "Geeks"]
 
# Sorting list of strings
words.sort(reverse=True)
 
print(words)


Output:

[4, 3, 2, 1]
[3.67, 3.28, 2.01, 2.0, 1.68]
['Geeks', 'Geeks', 'For']

Sorting List with Custom Function

To sort the list according to the user’s choice, pass a function to the key parameter of the Python sort() function.

Python3




# function to return the second element of the
# two elements passed as the parameter
def sortSecond(val):
    return val[1]
 
# list1 to demonstrate the use of sorting
# using second key
list1 = [(1,2),(3,3),(1,1)]
 
# sorts the array in ascending according to
# second element
list1.sort(key=sortSecond)
print(list1)
 
# sorts the array in descending according to
# second element
list1.sort(key=sortSecond,reverse=True)
print(list1)


Output:

[(1, 1), (1, 2), (3, 3)]
[(3, 3), (1, 2), (1, 1)]

Difference between sorted() and sort() function in Python

Let us see the difference between the sorted() and sort() function in Python:

Python sorted()

Python 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.
Syntax: sorted(iterable, key=key, reverse=reverse) Syntax: 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.
It can only sort a list that contains only one type of value. It sorts the list in place.

To know more please refer Python difference between sorted() and sort() function.


My Personal Notes arrow_drop_up
Last Updated : 09 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials