Last modified: Feb 02, 2026 By Alexander Williams

Python Sort Function Guide: Basics & Advanced

Sorting data is a core task in programming. Python makes it easy. It provides powerful built-in tools. You can order lists of numbers, strings, and complex objects. This guide covers everything you need to know.

We will explore the sorted() function and the list.sort() method. You will learn their differences and best uses. We will also dive into custom sorting logic.

Understanding sorted() vs. list.sort()

Python offers two main ways to sort. The sorted() function is a built-in. The list.sort() method belongs to list objects. Knowing which to use is key.

The sorted() function takes an iterable. It returns a new, sorted list. The original data remains unchanged. This is called an "out-of-place" operation.


# Example using sorted()
numbers = [3, 1, 4, 1, 5]
sorted_numbers = sorted(numbers)
print("Original list:", numbers)
print("New sorted list:", sorted_numbers)
    

Original list: [3, 1, 4, 1, 5]
New sorted list: [1, 1, 3, 4, 5]
    

The list.sort() method sorts the list in place. It modifies the original list directly. It does not create a new list. It returns None.


# Example using list.sort()
numbers = [3, 1, 4, 1, 5]
numbers.sort()
print("Sorted list (in-place):", numbers)
    

Sorted list (in-place): [1, 1, 3, 4, 5]
    

Choose sorted() when you need the original order. Use list.sort() when you want to save memory. It is also slightly faster for large lists.

Basic Sorting in Python

Both tools work intuitively. By default, they sort in ascending order. They handle numbers and strings seamlessly.


# Sorting numbers
scores = [88, 73, 92, 65]
scores.sort()
print("Sorted scores:", scores)

# Sorting strings
fruits = ["banana", "Apple", "cherry", "date"]
sorted_fruits = sorted(fruits)
print("Sorted fruits:", sorted_fruits)
    

Sorted scores: [65, 73, 88, 92]
Sorted fruits: ['Apple', 'banana', 'cherry', 'date']
    

Notice that 'Apple' comes first. Sorting is case-sensitive by default. Uppercase letters come before lowercase. We will fix this later.

Reverse Sorting

You often need descending order. Both functions have a reverse parameter. Set it to True.


numbers = [5, 2, 9, 1]
# Using sorted() with reverse
desc_numbers = sorted(numbers, reverse=True)
print("Descending (sorted):", desc_numbers)

# Using list.sort() with reverse
numbers.sort(reverse=True)
print("Descending (in-place):", numbers)
    

Descending (sorted): [9, 5, 2, 1]
Descending (in-place): [9, 5, 2, 1]
    

Custom Sorting with the 'key' Parameter

The real power comes from the key parameter. It lets you define how to sort. You pass a function. This function is applied to each element before comparison.

For a deeper understanding of how functions work in Python, see our Python Function Syntax Guide for Beginners.

Sort by string length: Use the built-in len function.


words = ["cat", "elephant", "dog", "bat"]
sorted_by_length = sorted(words, key=len)
print("Sorted by length:", sorted_by_length)
    

Sorted by length: ['cat', 'dog', 'bat', 'elephant']
    

Case-Insensitive Sort: Use the str.lower method.


fruits = ["banana", "Apple", "cherry", "date"]
case_insensitive_sort = sorted(fruits, key=str.lower)
print("Case-insensitive sort:", case_insensitive_sort)
    

Case-insensitive sort: ['Apple', 'banana', 'cherry', 'date']
    

Sort by a specific attribute: Use a lambda function. This is common with lists of dictionaries or objects.


students = [
    {"name": "Alice", "grade": 85},
    {"name": "Bob", "grade": 92},
    {"name": "Charlie", "grade": 78}
]
# Sort students by their grade
students_by_grade = sorted(students, key=lambda student: student["grade"])
print("Students by grade:", students_by_grade)
    

Students by grade: [{'name': 'Charlie', 'grade': 78}, {'name': 'Alice', 'grade': 85}, {'name': 'Bob', 'grade': 92}]
    

Lambda functions are small, anonymous functions. They are perfect for the key argument. For more advanced function techniques, including argument handling, check out our guide on Python Function Argument Unpacking.

Advanced Sorting Techniques

You can sort by multiple criteria. Return a tuple from the key function. Python sorts by the first item, then the second, and so on.


# Sort by last name, then first name
people = ["John Doe", "Jane Smith", "Alice Doe", "Bob Smith"]
sorted_people = sorted(people, key=lambda name: (name.split()[1], name.split()[0]))
print("Sorted by last, then first:", sorted_people)
    

Sorted by last, then first: ['John Doe', 'Alice Doe', 'Jane Smith', 'Bob Smith']
    

The Decorate-Sort-Undecorate Pattern: This is a classic technique. It involves creating a temporary list of tuples. This pattern is what the key parameter does internally.

Performance and Stability

Python's sort is a Timsort algorithm. It is very efficient and stable. A stable sort means items with equal keys keep their original order. This is crucial for multi-level sorting.