6.1 Sorting arrays

Download notebook Interact

We often need to sort arrays.

# Import the numpy package
import numpy as np

Here is an array of numbers:

my_array = np.array([9, 1, 3, 0])
my_array
array([9, 1, 3, 0])

Say we wanted to sort this array, from smallest to largest. To do this, we use np.sort:

sorted_array = np.sort(my_array)
sorted_array
array([0, 1, 3, 9])

That’s simple enough for numbers, but what happens if we have strings?

Here is an array of strings:

another_array = np.array(['Matthew', 'John', 'Peter'])
another_array
array(['Matthew', 'John', 'Peter'], dtype='<U7')

Notice the dtype - or data type. It is <U7. This means that the array contains strings of 7 characters or less. It’s not important for our purposes, but the “U” stands for Unicode - a way that the computer can store characters in strings.

What do you think would happen if we sorted another_array?

If you guessed that the strings get sorted alphabetically, you have a virtual gold star.

np.sort(another_array)
array(['John', 'Matthew', 'Peter'], dtype='<U7')

Remember how an alphabetical sort works. The string “a” comes before the string “aa”. “aa” comes before “ab”.

short_strings = np.array(['ab', 'aa', 'a'])
np.sort(short_strings)
array(['a', 'aa', 'ab'], dtype='<U2')

Here’s another array of strings:

further_array = np.array(['Matthew', 'Matt', 'Matty'])

Predict what order you will get from an alphabetical sort. Try it.