The NumPy (Numerical Python) library in Python allows users to store items in the form of arrays. NumPy arrays are extremely fast and memory efficient when compared to ordinary Python lists. In addition, the NumPy arrays provide a variety of built-in functions to perform complex numerical operations such as arithmetic, linear algebra, and geometry.

In this tutorial, we’ll teach you how to sort items stored in NumPy arrays. Specifically, we’ll give you examples for each of these scenarios:

  1. Sorting Numeric Arrays
  2. Sorting Text Arrays
  3. Sorting Boolean Arrays
  4. Sorting Two-Dimensional Arrays
  5. Sorting Arrays in Descending Order
    • Sorting One-Dimensional Arrays in Descending order
    • Sorting Two-Dimensional Arrays in Descending Order

Sorting Numeric Arrays

The sort() method from the NumPy module is used to sort NumPy arrays in Python. You can pass the array that you want to sort to the sort() method. The items in a numeric NumPy array are sorted in ascending order by default using the sort() method.

Here’s an example. The script below creates a NumPy array of 20 random integers between 1 and 50. The array is then sorted using the sort() method and the resultant sorted array is printed on the console.

import numpy as np

print("Array before sorting")

unsorted_array = np.random.randint(1,50,20)

print(unsorted_array)


print("\nArray after sorting")

sorted_array = np.sort(unsorted_array)

print(sorted_array)

Output:

Array before sorting
[42 17 30 49 32 27 45 36 41 44  7 19 23  7 12 30 30 31 40 13]

Array after sorting
[ 7  7 12 13 17 19 23 27 30 30 30 31 32 36 40 41 42 44 45 49]

Sorting Text arrays

You can use the sort() method to sort NumPy arrays contaning text items (strings), as well. NumPy arrays containing strings are sorted in alphabetical order of the item names. Here’s an example:

import numpy as np

print("Array before sorting")

unsorted_array = np.array(["Honda", "Toyota", "Ford", "BMW", "Mercedes"])

print(unsorted_array)


print("\nArray after sorting")

sorted_array = np.sort(unsorted_array)

print(sorted_array)

Output:

Array before sorting
['Honda' 'Toyota' 'Ford' 'BMW' 'Mercedes']

Array after sorting
['BMW' 'Ford' 'Honda' 'Mercedes' 'Toyota']

Sorting Boolean Arrays

The sort() method can also sort Boolean arrays. The sorted arrays will contain the “False” items before the “True” items as shown from the output of the following script:

import numpy as np

print("Array before sorting")

unsorted_array = np.array([True, False, True, True, False, False, False, True])

print(unsorted_array)


print("\nArray after sorting")

sorted_array = np.sort(unsorted_array)

print(sorted_array)

Output:

Array before sorting
[ True False  True  True False False False  True]

Array after sorting
[False False False False  True  True  True  True]

Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit

Sorting Two Dimensional Arrays

Sorting two-dimensional NumPy arrays is a little tricky as you can either sort items row-wise, or column-wise. By default, the sort() method applies row-wise sorting to two-dimensional arrays.

The following script creates a two-dimensional array of 3 rows and 4 columns. After sorting, you can see that the items in the first row are internally sorted. Similarly, the items in the second and third rows are also sorted.

import numpy as np

print("Array before sorting")  

unsorted_array = np.random.randint(1,50, size = (3,4))
print(unsorted_array)


print("\nArray after sorting")

sorted_array = np.sort(unsorted_array)

print(sorted_array)

Output:

Array before sorting
[[21 35  9 34]
 [45 23 27 34]
 [36  3 18 39]]

Array after sorting
[[ 9 21 34 35]
 [23 27 34 45]
 [ 3 18 36 39]]

To apply column-wise sorting to items in a two-dimensional array, you need to pass 0 as the value for the axis attribute of the sort() method. Here’s an example where the items in each of the 4 columns of a two-dimensional array are sorted.

import numpy as np

print("Array before sorting")  

unsorted_array = np.random.randint(1,50, size = (3,4))
print(unsorted_array)


print("\nArray after sorting")

sorted_array = np.sort(unsorted_array, axis = 0)

print(sorted_array)

Output:

Array before sorting
[[21 23  2 48]
 [20 38 49 25]
 [12 16  2 44]]

Array after sorting
[[12 16  2 25]
 [20 23  2 44]
 [21 38 49 48]]

Sorting Arrays in Descending Order

There is no direct method for sorting a NumPy array in descending order, but it’s still easy enough to do. One way to sort a NumPy array in descending order is to first sort the array in ascending order using the sort() function and then you can apply any array reversal technique to get the final array where the items are sorted in descending order. Let’s see how this is done.

Sorting one Dimensional Arrays in Descending order

To sort a one-dimensional array in descending order, you can first use the sort() method to sort the array in ascending order. After that, you can use the flipud() method which reverses the input sorted array. Here’s an example.

import numpy as np

print("Array before sorting")

unsorted_array = np.random.randint(1,50,20)

print(unsorted_array)


print("\nArray after sorting")

sorted_array = np.sort(unsorted_array)
sorted_desc = np.flipud(sorted_array)

print(sorted_desc)

In the output below, you can see that the numeric items are sorted in descending order.

Output:

Array before sorting
[ 8 46 12 13 48 25 34  6 34 14  6 23 46 22 17 40 31 15 33 47]

Array after sorting
[48 47 46 46 40 34 34 33 31 25 23 22 17 15 14 13 12  8  6  6]

Another way to sort a one-dimensional array in descending order is by using array indexing. The first step is to sort the array in ascending order using the sort() method. After that, you can apply the “[::-1]” array indexing on the sorted array to reverse the sorted array and get the final output array in descending order.

import numpy as np

print("Array before sorting")

unsorted_array = np.random.randint(1,50,20)

print(unsorted_array)


print("\nArray after sorting")

sorted_array = np.sort(unsorted_array)
sorted_desc = sorted_array[::-1]

print(sorted_desc)

Output:

Array before sorting
[45 28  8 27 21 19 23 43 14  4  1 33  6  5 12 15 24  1 35 46]

Array after sorting
[46 45 43 35 33 28 27 24 23 21 19 15 14 12  8  6  5  4  1  1]

Sorting Two-Dimensional Arrays in Descending order

You can sort two-dimensional NumPy arrays in descending order, either row-wise or column-wise, as well. Let’s show you how to do it both ways with these examples.

To sort a two-dimensional array in column-wise descending order, you can first sort the input array in the column-wise ascending order using thesort(input_array, axis = 0) method we described earlier. After that, pass the sorted array to the flipud() method to reverse the order of items in all the columns. The final output array will contain item in column-wise descending order. Look at the following example script:

import numpy as np

print("Array before sorting")

unsorted_array = np.random.randint(1,20, size = (3,4))  

print(unsorted_array)

print("\nArray after column-wise sorting ascending")

sorted_array = np.sort(unsorted_array, axis = 0)

print(sorted_array)

print("\nArray after column-wise sorting descending")

sorted_desc = np.flipud(sorted_array)

print(sorted_desc)

Output:

Array before sorting
[[18  3  7 17]
 [13 17  1 15]
 [18  8 14 10]]

Array after column-wise sorting ascending
[[13  3  1 10]
 [18  8  7 15]
 [18 17 14 17]]

Array after column-wise sorting descending
[[18 17 14 17]
 [18  8  7 15]
 [13  3  1 10]]

Finally, to sort a two-dimensional NumPy array in row-wise descending order, you first sort the input list in the row-wise ascending order using the sort() method. You can then call the fliplr() method to reverse the items in the sorted array from left to right. The final output array will contain items in row-wise descending order. Take a look:

import numpy as np

print("Array before sorting")

unsorted_array = np.random.randint(1,20, size = (3,4))  

print(unsorted_array)

print("\nArray after row-wise sorting ascending")

sorted_array = np.sort(unsorted_array)

print(sorted_array)

print("\nArray after row-wise sorting descending")

sorted_desc = np.fliplr(sorted_array)

print(sorted_desc)

Output:

Array before sorting
[[16  9  1 15]
 [16  5 10 10]
 [ 3  2 15  9]]

Array after row-wise sorting ascending
[[ 1  9 15 16]
 [ 5 10 10 16]
 [ 2  3  9 15]]

Array after row-wise sorting descending
[[16 15  9  1]
 [16 10 10  5]
 [15  9  3  2]]

If you found this tutorial helpful and you want to learn more about Python programming, enter your email address below and we’ll send you our best tips for getting the most out of Python:


Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit