Technology3 min read

Sorting a NumPy Array - numpy.sort() Function

Abhishek Ahlawat

By Abhishek Ahlawat โ€” Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#Python#Numpy#Numpy Sort

Summarize this article using AI

In our previous posts we learned what is Numpy and how to create a Numpy array. Now we will see how to sort the values stored in a given Numpy array. First of all, let us look at how to sort the array with the in-built sorted() method.

Sorting an array using sorted() function:

Let's create an array arr, having different values and then sort it.

import numpy as np

arr=np.array([51,65,2,7,100])    # NumPy array arr created
print(sorted(arr))    # Printing the sorted array

However, sorted() function is not an efficient method to sort a Numpy array.

Next, let us now sort the array using Numpy's sort function.


Using numpy.sort() function:

Intuitively, numpy.sort() is used to sort the values of a given NumPy array. There is only one argument necessary, that is obviously the array you want to sort. There are some other optional arguments which we will see later, let's first sort one array.

Following is the syntax for this function:

numpy.sort(array, ...)

In the code example below, are creating a NumPy array arr using np.random.randint() function. This array arr has a shape (2, 2) and is filled with values in the range 1 to 50. Then we sort this array using np.sort(arr) and sorted values are printed.

arr = np.random.randint(1,5) 
print('Original Array:',arr)

sorted_array = np.sort(arr)
print('Sorted Array is ',sorted_array)

As you can see above the array arr is sorted row-wise. The values in each row are compared with each other and sorted.

Now talking about the arguments, we passed the array to be sorted but there are some optional arguments too. These are:

1. axis: Axis along which the array needs to be sorted.

2. kind: The sorting algorithm which should be used to sort the array. The default is quicksort. We can pass mergesort or heapsort as values too.

3. order: This specifies which field to compare first. This is used in a Structured Numpy array. It can be a single column name or list of column names along which sorting needs to be done.


Time for some Examples:

Let's try a few examples to see the optional arguments in action.

Using axis parameter of numpy.sort() function:

import numpy as np

a = np.random.randn(2,3)

print("Original array :", a)

## axis = 0 means sort column wise
## axis = 1 means sort row wise
a_sort=np.sort(a, axis=0)

print("Sorted array :",a_sort)

Output:

Original array : [[-0.90271448  1.34187672 -0.36313977]
                  [-1.02900777 -0.56858467 -0.70997479]]
Sorted array : [[-1.02900777 -0.56858467 -0.70997479]
                [-0.90271448  1.34187672 -0.36313977]]

Using kind parameter of numpy.sort() function:

import numpy as np

b = np.random.randn(10,50)

print("Original Array :",b)

b_sort = np.sort(b, kind='heapsort')

print("Sorted array :", b_sort)

See the running example below,

The value for kind parameter can be: mergesort, heapsort, quicksort, stable, etc.

So, now we have sorted the NumPy array using both sorted() and np.sort() functions. You can also sort NumPy arrays by combining various optional arguments.

Happy Learning! Stay Curious!

You may also like:

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next