This article is part of in the series
Last Updated: Wednesday 29th December 2021

Sorting a list or tuple is easy in Python! Since a tuple is basically like an array that is not modifiable, we'll treat it almost the same as a list.

Sorting a Python List the Simple Way

Okay, so if you only want to sort a list of numbers, Python has a built in function that does all the hard work for you.

Say we have a list of numbers:

And we want to sort them in ascending order. All we do is call sort on the list, for in-place sorting, or the built in function sorted for not modifying the original list and returning a new sorted list. Both functions take in the same arguments and can be treated as "the same" for our purposes here, except for the reason above.

Here we go:

What about descending order?

Here you go:

What is Python doing behind the scenes? It's calling a version of mergesort on the list. It calls the function __cmp__ on each object when comparing values, and decides which one to put in front of the other based on the value returned from __cmp__. The return value is 0 for equal to, 1 for greater than, and -1 for less than the compared value. We'll use this information later to make our own objects sortable.

What about tuples, you say? I'm getting to that.

Sorting a Python Tuple the Simple Way

Since tuples are arrays that you cannot modify, they don't have an in-place sort function that can be called directly on them. They must always use the sorted function to return a sorted list. Keeping that in mind, here's how to do it:

Notice how sorted returns an array.

Okay, now let's see how to sort something a little more complicated.

Sorting a List of Lists or Tuples

This is a little more complicated, but still pretty easy, so don't fret! Both the sorted function and the sort function take in a keyword argument called key.

What key does is it provides a way to specify a function that returns what you would like your items sorted by. The function gets an "invisible" argument passed to it that represents an item in the list, and returns a value that you would like to be the item's "key" for sorting.

Let me illustrate, for your superb eyes only, the key keyword argument!

So, taking a new list, let's test it out by sorting by the first item in each sub-list:

Here we can see that the list is now sorted by the first item in the sub-list in ascending order. Note that you could have used the sort function as well, but I personally like the sorted function better, so I'll be using that in further examples.

What happened? Remember that "invisible" argument I was talking about? That's what gets passed into the getKey function every time the sorted needs a value. Tricky, tricky python ;).

Sorting by the second item in each sub-list would be as simple as changing the getKey function to this:

Alright. All good. Now, what about a list of tuples? Glad you asked!

It's actually the exact same as our example above, but has the list defined as such:

The only thing that has changed is that we now get a list of tuples instead of a list of lists returned to us.

The exact same solution can be applied to a tuple of tuples, so I'm not going to go there, as that would just be redundant. It would also waste more of those digital trees that make this beautiful digital paper.

Sorting a List (or Tuple) of Custom Python Objects

Here's a custom object that I created:

Let's make a list of them, for sorting purposes:

Okay, so we've got all of our fancy new custom objects in a list and we want to be able to sort them. How do we do that?

Well, we could define a function, like we did above, that takes in the item and returns a list. So let's do that.

This one is a little different, because our object is not a list anymore. This allows us to sort by the number property in the custom object.

So if we run the sorted function on our list of fancy custom objects, we get this:

A big mess of ugliness that we don't understand. Perfect. But don't worry, dear viewer, there is a simple fix we can apply to our custom object to make it all better!

Let's redefine our object class like this:

Ok, what the heck did we just do? First, the __repr__ function tells Python how we want the object to be represented as. In more complex terms, it tells the interpreter how to display the object when it is printed to the screen.

So, we tell Python to represent the object by it's class name, name, and number.

Now lets try sorting it again:

That's much better! Now we can actually tell that it sorted properly!

But there's a little bit of a problem. It's just nit-picking, but I don't want to have to type that key keyword every time I call sorted.

So, how do I do that? Well, remember that __cmp__ function I told you about? Let's put it into action!

Let's redefine our object one more time like this:

Looking good. What this does is tell Python to compare the value of the current object to another object in the list to see how it compares. Like I said above, the sorted function will call the __cmp__ function on the objects it is sorting in order to determine where they should be in relation to other objects.

Now we can just call sorted without worrying about including the key keyword, like so:

And voilà! It works nicely. Please note that all of the above also applies to tuples of the custom object. But I like to save my digital trees, as you know.

Sorting a Heterogeneous List of Custom Python Objects

Alright. Since Python is a dynamic language, it doesn't so much care about what objects we throw into lists. They can all be the same type, or they can all be different.

So let's define another different object to use with our Custom object.

It's a similar object, but still different from our Custom object.

Let's make a list of these and our Custom objects:

Now if we try to run sorted on this list:

We get a lovely error. Why? Because Custom doesn't have an attribute called age and AnotherObject doesn't have an attribute called number.

What do we do? Panic!

Just kidding. We know what to do. Let's redefine those objects again!

Awesome! What did we just do? We defined a common getKey function that both of the objects have so that we can compare them easily.

So now if we run the sorted function again, we get:

Nice! Now our objects can be compared and sorted to their hearts content.

You still prefer the using the key keyword, you say?

You can do that too. If you leave out the __cmp__ functions in each object, and define an outside function like so:

And then call sorted like so:

And there you have it! Pretty straight forward, but not as straight forward as some might guess. Still, Python makes it pretty easy with its built in sorted function.

For more sorting ideas, head over to How to Sort Python Dictionaries by Key or Value. You could also check out Lambda Function Syntax (Inline Functions) in Python for instructions on how to sort with lambda functions.

And there you are. One step closer to becoming the world's Python master.

Sayonara, for now.

About The Author