Table of contents
1.
Introduction
2.
What is Python list sort?
2.1.
Python List sort() Syntax
2.2.
Optional two parameters of the prompt function
3.
List sort() in Python Examples
3.1.
Sort a List of Numbers in Ascending Order
3.2.
Python
3.3.
Sort a List of Numbers in Descending Order
3.4.
Python
3.5.
Sort() Function Using The Key Parameter
3.6.
Python
3.7.
Sorting Elements of Different Data Types Using a Custom Function
3.8.
Python
4.
Alternatives to the sort method in Python
4.1.
sorted() function
4.2.
numpy sort() function
5.
Frequently Asked Questions
5.1.
What is the difference between sort() and sorted() functions in Python? 
5.2.
Different data types can be sorted by the sort() function or not.
5.3.
Can the sort() function sort a list that contains duplicates?
6.
Conclusion
Last Updated: Aug 21, 2025
Easy

Python List sort()

Author Dhruv Rawat
0 upvote

Introduction

The sort() method in Python is used to arrange list elements in a specific order. With sort in Python, you can organize data in ascending, descending, or even custom order, making it a flexible tool for handling lists effectively.

 

python list sort

What is Python list sort?

It is a built-in function in Python used to sort the list of items in place in ascending or descending order. With this sort() function in Python, we can sort the list according to our needs and apply it to any data types. The time complexity of the list sort in each case is O(n*logn).

Python List sort() Syntax

The syntax for the Python List sort function is as follows:-

name_of_list.sort(reverse=False, key=None)

Optional two parameters of the prompt function

Let's discuss each parameter used in sort() method in Python

  • reverse: This parameter determines the Sorting order of the list. The default value is reverse=False, which means sorting the list in ascending order. If reverse=True, the list sorts in descending order.
     
  • key: This parameter specifies a custom function to determine the sorting criteria. It takes a single argument and returns a value to sort the list. The key's default value is None, which sets the list to sort on their default ordering 

List sort() in Python Examples

In this section, we are implementing Python List sort and its various use cases.

Sort a List of Numbers in Ascending Order

In this example, we will be sorting a list of numbers in ascending order.

  • Python

Python

array = [11, 32, 41, 23]
# Sorting list in ascending order
array.sort()
# printing the list
print('Sorted list in Ascending order :', array)
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

Sort a List of Numbers in Descending Order

In this example, we will sort a list of alphabets in descending order.

  • Python

Python

sentence = ['coding', 'is', 'love', 'ninja']
# sort in descending order
sentence.sort(reverse=True)
# printing the list
print('Sorted Alphabets in Descending:', sentence)
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

Sort() Function Using The Key Parameter

In this example, we will perform custom sorting through the sort() function using the key parameter.

  • Python

Python

institute = ['Apple', 'ION', 'Coding Ninjas', 'Atlassian', 'Google']
# function to use as the sorting key
def sorting_length(item):
   return len(item)
#calling the function
institute.sort(key=sorting_length)
# Print the sorted list according to length
print(institute)
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

Sorting Elements of Different Data Types Using a Custom Function

In this example, we are sorting elements of different data types using a custom function.

  • Python

Python

list = [0, 5.6, "coding", True, None,{"Python": 3, "C++": 5}]
# custom function define
def cust_sort(item):
   if isinstance(item, int):
       return 0
   elif isinstance(item, float):
       return 1
   elif isinstance(item, str):
       return 2
   elif isinstance(item, dict):
       return 3
   elif item is None:
       return 4
   else:
       return 5
# custom function
list.sort(key=cust_sort)
# Print
print("Sorted list is here ->", list)
You can also try this code with Online Python Compiler
Run Code

 

Output:

Output

Alternatives to the sort method in Python

Let's see some more methods that work the same way as sort() in Python

sorted() function

It is a built-in function in Python that returns a sorted list from an iterable object. It does not modify the original list, unlike the sort() function. The syntax is:
 

sorted(iterable_object, key=None, reverse=False)

numpy sort() function

It is part of the numpy library in Python and returns a sorted list without modifying the actual list. The syntax is
 

numpy.sort(a, axis=-1, kind=None, order=None)


Here, the axis is the axis along which the array is to be sorted, kind is the sorting algorithm to use ('quicksort,' mergesort,' or 'heapsort'), and order is the field to sort structured arrays.

Frequently Asked Questions

What is the difference between sort() and sorted() functions in Python? 

The sort() function modifies the original list and the sorted() function does not modify the original list, which means in the sort() function, the original list gets changed while the other one returns a sorted list, not changing the original list. Hence sort() function performs an in-place sorting operation and saves us memory.

Different data types can be sorted by the sort() function or not.

Yes, different data types can be sorted by sort() function. We need to pass a key parameter to the custom function that we made which will return a value that can be used for comparison to sort the elements. See above example 4, we have implemented a custom function.

Can the sort() function sort a list that contains duplicates?

If the list contains duplicates then the sort() will sort the list according to the appearance of the elements in the original list. So if we have a list [1,2,3,2], on performing sort(), it will give [1,2,2,3] as output.

Conclusion

Congratulations on finishing this article!! This article covered the syntax of the Python List sort() function, and then we looked at the sorting criteria of the Python List sort. We have also discussed examples of how to implement the sort() method and looked at alternatives for the sort() method.

Recommended Readings:

Live masterclass