Introduction

Welcome to the lesson on mastering Python's built-in sorting function! By this point, you've likely realized that sorting isn't merely an abstract mathematical operation but a substantial real-world necessity. Sorting influences how we understand data, how we locate specific data entries in large datasets, and how efficiently we can use our computational resources.

Consider an e-library system where thousands of books are stored, for example. Sorting these books based on their titles or authors not only makes the database more organized but also permits faster searching and accessing of specific books. In this lesson, we'll explore several such scenarios where Python's built-in sorted() function comes to our rescue. Let's get started!

Problem 1: Sorting Values in a List

As a starting point, let's consider a familiar task where you have a list of integers generated randomly. You need to sort this list in ascending order. In our e-library example, this task could be likened to arranging the books based on their unique ID numbers.

Python has a built-in function called sorted() that sorts a given list without modifying the original one. Instead, it returns a new list with the elements of the original list in sorted order. Here's how we can solve this problem:

def sort_list(values):
    return sorted(values)

Using the built-in sorted() function, we've sorted the list easily and efficiently.

Problem 2: Sorting Values in a List in Reverse Order

Next, suppose you need to sort a list of integers, but this time in descending order. For instance, you might want to arrange the e-library's books based on their publication year, with the most recent ones appearing first.

The sorted() function is again handy here, but we need to set its reverse argument to True. Here's how to do that:

def sort_list(values):
    return sorted(values, reverse=True)

Setting the reverse parameter to True instructs Python to sort the elements in descending order, a departure from the default ascending order.

Problem 3: Sorting Tuples by the Second Element

Next, consider a situation where you need to sort a list of tuples. Each tuple contains two elements — an integer and a string (for instance, the integer might be a unique ID representing a book, and the string is the book's title). You want to arrange these tuples based on the strings.

The sorted() function can sort complex data structures like tuples using the key parameter. This parameter defines a function that takes an input element and returns a key that Python will use for sorting purposes. To sort the tuples based on the second element (i.e., the string), we'll use a lambda function as the key. Here's your solution:

def sort_tuples(tuples):
    return sorted(tuples, key=lambda x: x[1])

The lambda function x: x[1] takes an element from tuples and returns its second element (i.e., x[1]). The sorted() function uses these second elements to sort the tuples.

On top of that, if the second element can include ties we need to eliminate, a tuple comes to the rescue, as tuples in Python are automatically comparable:

def sort_tuples_ties(values):
    return values.sort(key=lambda x: (x[1], x[0]))

This code will now sort the values list, first sorting by the x[1] value and, in case of a tie, sorting by the x[0] value.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal