Hey, readers! In this article, we will be focusing on **NumPy sorting techniques**, in detail.

So, let us begin!! 🙂

Table of Contents

## NumPy module – Quick Overview

Python consists of various modules to perform variations with the data and mold the same according to the requirements.

With NumPy module, the mathematical computation of data has become very easy. It offers us with various functions for mathematical computation of the data values at ease.

It offers us with array data structure to store the data and perform manipulations over it. When it comes to dealing with elements, NumPy offers us with functions to sort the entire array to make it available for processing at ease.

Let us have a look at them in the upcoming section.

## NumPy Sorting methods

When it comes to array, we can make use of sorting methods to sort the array in an order plus manipulate them for further use.

In context of this topic, we will be focusing on the below functions as a part of this concept–

**sort() function****lexsort() function****argsort() function**

### 1. NumPy sort() function

The sort() function enables us to sort the NumPy Array in a customized manner. That is, we can have the NumPy array sorted in an ascending or descending order of fashion.

Plus, it gives us the customization to choose the axis of the sorting space.

**Syntax–**

```
numpy.sort(array, axis)
```

With axis = None, the sorting of the elements happen in a traditional fashion and thus the result of the array is a one line or single row of elements.

But, when the axis is set to 1, the row-wise sorting happens and the elements of the array gets sorted in a row wise fashion also known as sorting per row of the array structure.

**Example 01–**

In this example, as the axis = None, both the rows of the input array are treated as a single row while sorting. As a result, it sorts the entire array as considering it as a 1-D array and then prints the results also in 1-D ascending order format.

```
import numpy as np
num = np.array([[20, 10], [0, 11]])
ans = np.sort(num , axis = None)
print ("Data before sorting :", num)
print("Data after sorting with axis=None:", ans)
```

**Output–**

```
Data before sorting: [[20 10]
[ 0 11]]
Data after sorting: [ 0 10 11 20]
```

**Example 02–**

As we have now set axis = 1, it would perform sorting per row within the array and save results into those positions itself as shown below–

```
import numpy as np
num = np.array([[20, 10], [0, 11]])
ans = np.sort(num , axis = 1)
print ("Data before sorting :", num)
print("Data after sorting with axis=1:", ans)
```

**Output–**

```
Data before sorting: [[20 10]
[ 0 11]]
Data after sorting with axis=1:[[10 20]
[0 11]]
```

### 2. The lexsort() method

With NumPy lexsort() method, we can easily sort the data values with respect to column unlike sort() method. Yes, lexsort() function makes use of sequence of keys to sort the data effectively. Thus one at a time consideration for every element happens here.

Also, as a result, we receive the index of the sorted elements in an ascending order.

**Example–**

```
import numpy as np
num = np.array([2,1,0,10])
num1 = np.array([1,2,3,-8])
res = np.lexsort((num1, num))
print("Sorted index values of the array:", res)
```

**Output–**

```
Sorted index values of the array: [2 1 0 3]
```

### 3. The argsort() sorting method

NumPy agrsort() function performs sorting on the array elements and returns the indexes of the sorted array in an ascending order. It works in a similar fashion as that of sort() function with axis=None argument, but instead of returning the actual array elements, it returns the index values of those arrays.

**Example–**

```
import numpy as np
num = np.array([2,1,0,10])
res = np.argsort((num))
print("Sorted index values of the array:", res)
```

**Output–**

```
Sorted index values of the array: [2 1 0 3]
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 🙂