There are a lot of sorting algorithms such as Bubble sort, Insertion sort, Merge sort, Counting sort, etc. But most of the time, we use builtin sorting function to sort an iterable (e.g. list, tuple, dictionary, custom class with iterator etc.).

Today we will sort a given list of strings in several ways to understand the Python 3 built-in sorted function .

Dataset

We will use the 50 U.S. states as our dataset.

states = [
    "Alabama", "Alaska", "Arizona", "Arkansas",
    "California", "Colorado", "Connecticut",
    "Delaware",
    "Florida",
    "Georgia",
    "Hawaii",
    "Idaho", "Illinois", "Indiana", "Iowa",
    "Kansas", "Kentucky",
    "Louisiana",
    "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota",
    "Mississippi", "Missouri", "Montana",
    "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
    "New York", "North Carolina", "North Dakota",
    "Ohio", "Oklahoma", "Oregon",
    "Pennsylvania",
    "Rhode Island",
    "South Carolina", "South Dakota",
    "Tennessee", "Texas",
    "Utah",
    "Vermont", "Virginia",
    "Washington", "West Virginia", "Wisconsin", "Wyoming"
]

Sorting Techniques

Now we will sort the above states list in different ways.

Sort list alphabetically in ascending order:

To sort the given list alphabetically in ascending order, we pass the list variable states to the builtin function sorted.

states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
          'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
          'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
          'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
          'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
          'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
          'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
          'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
          'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
          'West Virginia', 'Wisconsin', 'Wyoming']
sorted_states_alphabetic_ascending = sorted(states)
print(sorted_states_alphabetic_ascending)

Output:

['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']

Sort list alphabetically in descending order:

To sort the given list alphabetically in descending order, we pass the list variable states to the builtin function sorted and set the reverse parameter to True.

states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
          'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
          'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
          'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
          'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
          'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
          'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
          'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
          'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
          'West Virginia', 'Wisconsin', 'Wyoming']
sorted_states_alphabetic_descending = sorted(states, reverse=True)
print(sorted_states_alphabetic_descending)

Output:

['Wyoming', 'Wisconsin', 'West Virginia', 'Washington', 'Virginia', 'Vermont', 'Utah', 'Texas', 'Tennessee', 'South Dakota', 'South Carolina', 'Rhode Island', 'Pennsylvania', 'Oregon', 'Oklahoma', 'Ohio', 'North Dakota', 'North Carolina', 'New York', 'New Mexico', 'New Jersey', 'New Hampshire', 'Nevada', 'Nebraska', 'Montana', 'Missouri', 'Mississippi', 'Minnesota', 'Michigan', 'Massachusetts', 'Maryland', 'Maine', 'Louisiana', 'Kentucky', 'Kansas', 'Iowa', 'Indiana', 'Illinois', 'Idaho', 'Hawaii', 'Georgia', 'Florida', 'Delaware', 'Connecticut', 'Colorado', 'California', 'Arkansas', 'Arizona', 'Alaska', 'Alabama']

Sort list by string length

We can also sort the list using another builtin or custom function as a key parameter. The function which is mentioned in the key parameter will be called on each list item before making comparisons.

The following snippet shows how we can sort the given list by the length of the items in ascending order using the key parameter. Here we use the builtin len function as the comparator function.

states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
          'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
          'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
          'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
          'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
          'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
          'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
          'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
          'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
          'West Virginia', 'Wisconsin', 'Wyoming']
sorted_states_length_ascending = sorted(states, key=len)
print(sorted_states_length_ascending)

Output:

['Iowa', 'Ohio', 'Utah', 'Idaho', 'Maine', 'Texas', 'Alaska', 'Hawaii', 'Kansas', 'Nevada', 'Oregon', 'Alabama', 'Arizona', 'Florida', 'Georgia', 'Indiana', 'Montana', 'Vermont', 'Wyoming', 'Arkansas', 'Colorado', 'Delaware', 'Illinois', 'Kentucky', 'Maryland', 'Michigan', 'Missouri', 'Nebraska', 'New York', 'Oklahoma', 'Virginia', 'Louisiana', 'Minnesota', 'Tennessee', 'Wisconsin', 'California', 'New Jersey', 'New Mexico', 'Washington', 'Connecticut', 'Mississippi', 'North Dakota', 'Pennsylvania', 'Rhode Island', 'South Dakota', 'Massachusetts', 'New Hampshire', 'West Virginia', 'North Carolina', 'South Carolina']

Suppose we need to sort the above list by the item length in descending order. In this case, we can set the reverse parameter to False along with the key parameter.

states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
          'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
          'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
          'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
          'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
          'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
          'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
          'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
          'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
          'West Virginia', 'Wisconsin', 'Wyoming']
sorted_states_length_descending = sorted(states, key=len, reverse=True)
print(sorted_states_length_descending)

Output:

['North Carolina', 'South Carolina', 'Massachusetts', 'New Hampshire', 'West Virginia', 'North Dakota', 'Pennsylvania', 'Rhode Island', 'South Dakota', 'Connecticut', 'Mississippi', 'California', 'New Jersey', 'New Mexico', 'Washington', 'Louisiana', 'Minnesota', 'Tennessee', 'Wisconsin', 'Arkansas', 'Colorado', 'Delaware', 'Illinois', 'Kentucky', 'Maryland', 'Michigan', 'Missouri', 'Nebraska', 'New York', 'Oklahoma', 'Virginia', 'Alabama', 'Arizona', 'Florida', 'Georgia', 'Indiana', 'Montana', 'Vermont', 'Wyoming', 'Alaska', 'Hawaii', 'Kansas', 'Nevada', 'Oregon', 'Idaho', 'Maine', 'Texas', 'Iowa', 'Ohio', 'Utah']

Sort list by using lambda function

We can use the lambda function in the key parameter.

The following snippet shows how we can sort the given list by the number of a's in the state name in descending order.

states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
          'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
          'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
          'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
          'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
          'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
          'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
          'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
          'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
          'West Virginia', 'Wisconsin', 'Wyoming']
sorted_states_a_descending = sorted(states,
                                    key=lambda x: x.lower().count('a'),
                                    reverse=True)
print(sorted_states_a_descending)

Output:

['Alabama', 'Alaska', 'Arkansas', 'Arizona', 'California', 'Delaware', 'Hawaii', 'Indiana', 'Kansas', 'Louisiana', 'Maryland', 'Massachusetts', 'Montana', 'Nebraska', 'Nevada', 'North Carolina', 'North Dakota', 'Oklahoma', 'Pennsylvania', 'South Carolina', 'South Dakota', 'Colorado', 'Florida', 'Georgia', 'Idaho', 'Iowa', 'Maine', 'Michigan', 'Minnesota', 'New Hampshire', 'Rhode Island', 'Texas', 'Utah', 'Virginia', 'Washington', 'West Virginia', 'Connecticut', 'Illinois', 'Kentucky', 'Mississippi', 'Missouri', 'New Jersey', 'New Mexico', 'New York', 'Ohio', 'Oregon', 'Tennessee', 'Vermont', 'Wisconsin', 'Wyoming']

Here in the lambda function, we traverse each name using the variable x. Then we convert the state name into lowercase letters and count the number of the letter a in each state name.

Sort list by using custom comparator method using cmp_to_key

Apart from the lambda function, we can also use a custom function in the key parameter. To do that, we use the cmp_to_key from the functools module to enable us to use a custom comparator.

Suppose, we can sort the given list by the string length in descending order. If two states have the same length, we want to place the states in alphabetically descending order.

For example, Ohio will be placed before Alabama, as it is smaller. On the other hand, Utah and Ohio have the same length. But in this case, we want to place Utah before Ohio as Utah is alphabetically larger than Ohio.

from functools import cmp_to_key


def compare_two_states(state_1, state_2):
    if len(state_1) < len(state_2):
        return -1
    elif len(state_1) > len(state_2):
        return 1
    if state_1 > state_2:
        return -1
    elif state_1 < state_2:
        return 1
    return 0


states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
          'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
          'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
          'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
          'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
          'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
          'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
          'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
          'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
          'West Virginia', 'Wisconsin', 'Wyoming']
sorted_states_custom = sorted(states,
                              key=cmp_to_key(compare_two_states))
print(sorted_states_custom)

Output:

['Utah', 'Ohio', 'Iowa', 'Texas', 'Maine', 'Idaho', 'Oregon', 'Nevada', 'Kansas', 'Hawaii', 'Alaska', 'Wyoming', 'Vermont', 'Montana', 'Indiana', 'Georgia', 'Florida', 'Arizona', 'Alabama', 'Virginia', 'Oklahoma', 'New York', 'Nebraska', 'Missouri', 'Michigan', 'Maryland', 'Kentucky', 'Illinois', 'Delaware', 'Colorado', 'Arkansas', 'Wisconsin', 'Tennessee', 'Minnesota', 'Louisiana', 'Washington', 'New Mexico', 'New Jersey', 'California', 'Mississippi', 'Connecticut', 'South Dakota', 'Rhode Island', 'Pennsylvania', 'North Dakota', 'West Virginia', 'New Hampshire', 'Massachusetts', 'South Carolina', 'North Carolina']

The custom comparator should return an integer/float value that follows the following pattern:

  • return a negative value (<0) when the left item should be sorted before the right item.
  • return a positive value (>0) when the left item should be sorted after the right item.
  • return zero (0) when both the left and the right items have the same weight and should be treated equally.

Practice challenges

Medium difficulty challenges:

Reference

Advertisement

Citation

Click to select citation style

Shovon, A. R. (2022, January 16). Sorting a list (Python 3). Ahmedur Rahman Shovon. Retrieved December 3, 2024, from https://arshovon.com/blog/sorting-a-list-python3/

Shovon, Ahmedur Rahman. “Sorting a list (Python 3).” Ahmedur Rahman Shovon, 16 Jan. 2022. Web. 3 Dec. 2024. https://arshovon.com/blog/sorting-a-list-python3/.

@misc{ shovon_2022,
    author = "Shovon, Ahmedur Rahman",
    title = "Sorting a list (Python 3)",
    year = "2022",
    url = "https://arshovon.com/blog/sorting-a-list-python3/",
    note = "[Online; accessed 3-December-2024]"
}
Related contents in this website