Understanding Sorted Function in Python: A Simple Guide

Introduction

If you‘ve written more than a few lines of Python code, chances are you needed to sort some data at some point. Knowing how to properly use the built-in sorted() function is essential.

Sorted allows you to order elements of any iterable object like lists, tuples, and dictionaries. It returns a new sorted list copy while preserving the original.

Let‘s explore some examples of where sorting data with sorted() can be useful:

  • Ordering log files by timestamp to analyze trends over time
  • Sorting products by price to display cheapest first
  • Organizing social media data by number of likes

The key thing is that sorting allows easier searching, analysis, and presentation of information.

In this comprehensive guide, you‘ll learn:

  • How to use sorted() parameters like key and reverse
  • Best practices for sorting more complex data structures
  • Advanced techniques like creating reusable custom keys
  • Performance characteristics of sorted()

So whether you‘re just getting started with Python or have some experience, this guide will level up your skills with sorted()!

Syntax and Parameters

Let‘s start by understanding the syntax and arguments for sorted():

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

Where:

  • iterable (required): The sequence to sort – list, tuple, dictionary etc.
  • key: An optional function that transforms each element before comparison
  • reverse: Optional boolean that reverses order if True (default False)

Note sorted() will work on any iterable sequence, but always returns a list containing the sorted elements.

Now let‘s look at some examples!

Sorting Basics like Lists and Strings

For basic data types like numbers, strings, and generic iterables, sorted() can sort things in ascending order out of the box…

sorted([5, 2, 8, 3]) # [2, 3, 5, 8]

countries = ["France", "Germany", "Belgium"]
sorted(countries) # [‘Belgium‘, ‘France‘, ‘Germany‘]  

sorted("Hello World") # [‘ ‘, ‘H‘, ‘W‘, ‘d‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘o‘, ‘r‘]

When sorting strings, the ASCII value of each character is used for ordering. This explains why capital letters come before lower case letters.

Sorting Dictionaries with Custom Keys

To sort more complex data structures like nested lists or dictionaries, we need to provide a custom sort key to guide sorted() on how to order elements meaningfully.

Let‘s take a list of dictionaries representing people with name and ages:

people = [
    {"name": "John", "age": 28},
    {"name": "Ana", "age": 33},
    {"name": "Lisa", "age": 42}
]

To sort by age, we extract just the age as the key:

def get_age(person):
    return person["age"]

sorted(people, key=get_age)  
# Sorts people by age

For one-off sorting, lambda functions are useful anonymous inline functions:

sorted(people, key=lambda person: person["name"]) 
# Sorts people by name

You can also chain multiple keys for more advanced behavior.

Performance Characteristics

Sorted() is an optimized stable sorting algorithm with guaranteed O(n log n) time complexity in the average and worst case. This makes it extremely fast compared to otherPython sorting options.

It achieves its speed partly by being built in C, but also by its hybrid algorithm combining merge sort and insertion sort.

Additionally, since sorted creates a new list rather than operating in-place, it has O(n) space complexity.

When to use sorted() vs sort()

  • Use sorted() when you want to preserve original ordering of elements.
  • Use sort()/list.sort() for in-place sorting that modifies the input.

There‘s rarely a good reason to implement your own basic sorting algorithm in Python because sorted() will outperform it!

However for very large data sets of millions of elements, or requiring parallel processing, a dedicated high performance sorting library may be better suited.

Going Pro with Sorted()

Let‘s round out this guide by looking at some more advanced capabilities of Python‘s awesome sorted function!

Leveraging Decorators as Keys

Since the key parameter takes any callable, we can use decorated functions:

from functools import cmp_to_key

@cmp_to_key
def compare_lengths(str1, str2):
    if len(str1) < len(str2):
        return -1 
    elif...

sorted(strings, key=compare_lengths) 

This allows reusable encapsulated comparators.

Controlling Sort Stability

Sorted is stable by default meaning that records with equal keys will remain in original input order.

Implementing unstable algorithms like quicksort can improve performance for some cases.

Best Practices for Mixing Types

When sorting mixed types in iterables, be careful to define keys that transform everything to consistent types for comparison.

Numbers and strings don‘t directly compare:

sorted([5, "2", 8, "3"]) # Error comparing int and str

In conclusion, knowing how to harness the power of sorted() can greatly streamline your Python projects and improve efficiency working with data.

Check out the Python Sorting HOW-TO for even more advanced use cases this guide didn‘t cover. Happy sorting!