How to Sort Objects of a Class in Python



Python


In this article, we show how to sort objects of a class in Python.

Sorting objects in Python is not as straightforward as sorting other data types such as integers.

The reason it isn't is because objects normally contain many different attributes. For example, for student objects (representing students in a school), attributes may include name, age, grade, etc.

You simply can't just use the sorted() function, because Python won't know what attribute you want to sort the objects based off of. Therefore, you have to specify which attribute you want to sort the object.

This can be done in a number of ways.

In this article, we will show how to sort objects with custom functions, lambda functions, or the attrgetter function.


Custom Functions

So we will now go over ways we can sort objects of a class in Python, first starting with doing so with custom functions.

So with custom functions, we can sort objects of a class according to any attribute.

Below we create a class called Alligator.

We define an __init__ method that passes in the parameters, self, color, length, mood, and age.

We then create 3 Alligator objects.

We then create a list of these 3 Alligator objects.

We then sort this list with a custom function that we write.

All of this is shown in the code below.



So let's now go over the code.

So we create a class called Alligator, with an __init__ method that accepts the parameters, self, color, length, mood, and age. We create a __repr__ method for debugging purposes of improving human readability.

We then define 3 Alligator objects.

We put these in a list called alligators.

This is the list we will sort throughout our code.

We do this by creating a custom functions.

sortalligators_color() sorts the objects by color.

sortalligators_length() sorts the objects by length.

sortalligators_mood() sorts the objects by mood.

sortalligators_age() sorts the objects by age.

We then create variables for all the new lists that will be sorted by all these attributes.

sorted_alligators_color contains the sorted list according to the sortalligators_color function.

sorted_alligators_length contains the sorted list according to the sortalligators_list function.

sorted_alligators_mood contains the sorted list according to the sortalligators_mood function.

sorted_alligators_age contains the sorted list according to the sortalligators_age function.

So if we run the above code, we get the following output, shown below.



So you can see all of the lists sorted according to the various attributes.


If you wanted to sort the lists in reverse order, you would have the following code, shown below.



So any list that you want sorted in reverse in Python, we add the statement, reverse= True, to the sorted() function.


Lambda Functions

Creating custom functions is not the only way to sort objects of a class in Python.

Another way objects of a class can be sorted is through lambda functions.

A lambda function is an anonymous function that can sort objects by the attribute that the anonymous function references.

With a lambda function, unlike a custom function, we don't literally create the function. We don't create any function as we would in Python with the def keyword followed by a function name. With a lambda function, we simply add an anonymous function have it reference the attribute we want.

Let's see how this works in the code shown below.



So this code is actually much simpler.

We create the same class, along with the same objects.

We then put the objects in a list.

We then create a variable sorted_alligators_color, which sorts the list of alligators by the color attribute.

We then print out the variable.

If we wanted to order the list in reverse order, we would have the following modify the variable sorted_alligators_color to the following below.



And we can do this for any variable. Notice how we don't create a function e. This function is a lamba function. It is anonymous. It is used in this code simply as an anonymous function to return the color attribute of the alligator objects.


Attrgetter Function

There is still yet another way we can sort objects of a class in Python.

Another way we can do is through the use of the attrgetter() function in Python.

This function can get the attribute of an object so that the sorted() function can sort the objects of the class.

This is shown in the code below.



So the first thing we do in our code is import attrgetter from operator.

Now that attrgetter is imported, we can use it to sort an attribute obtained from the attrgetter function.

We have our same class, objects, and list of objects.

We then create the variable sorted_alligators_age and set it equal to, sorted(alligators, key=attrgetter('age'))

This sorts the objects of the class according to the age attribute.

If we want to sort the objects in reverse order, the sorted_alligators_age variable would be modified as follows.