A nerd pretending to be a software engineer.

Python – Custom List Sorting Function

Written on

There are several ways to sort a list, where the basic ones is to use the list.sort()  method to sort the list without returning anything, or use the sorted() function that returns a new sorted list, but this mostly works only on list that contains basic data types, if we want to sort a list that contains something else (another list, tuple, dictionary, custom classes, etc), we need to define a custom sorting function.

Sorting in Python is pretty simple, the list class provides a sort() function that we can use by default.

ls = [2,3,1,4]
print ls #[1,2,3,4]

One of the example of scenario where we need a custom sorting function is where we have custom data structure and we want to define which of the class attribute that is used as they sorting key, in this case, we will specify the key argument of the sorting function. In the examples, the list.sort() method is used, but it can be used with the sorted() function as well.

# consider we want to sort a list of string by its length

ls = ['car','television','laptop','ok']

# specify key=len , for the len() function that will
# return its length given a string

print ls # ['ok','car','laptop','television']

The key arguments takes a function name that takes an argument and returns an object that will be used as the key, for example, to define a custom key function:

#for example, we want to sort a list of tuples based on its
# last element:

def lastElement(tuple):
    return tuple[-1]

ls = [(3,4),(1,2),(5,3),(8,1)]
ls.sort(key = lastElement)
print ls #[(8,1),(1,2),(5,3),(3,4)]

In addition, you can specify the reverse argument to true if you want it to sort the list in descending order.

ls = [1,3,2,4]
print ls #[4,3,2,1]
comments powered by Disqus