Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

3 Easy Sorting Techniques in NumPy

  • Safa MulaniSafa Mulani
  • March 31, 2021
  • NumPy, Python Programming
3 Easy Sorting Techniques in NumPy

Hello, readers! In this article, we will be focusing on 3 Sorting techniques in NumPy, in detail.

So, let us begin! 🙂

Python NumPy library offers us various functions to create an array and manipulate elements of a similar type in an array structure. Along with this, NumPy offers us various functions that can enable us to sort the elements present in the array structure.

Sorting Techniques in NumPy

We’ll learn the below sorting techniques in NumPy.

  1. NumPy sort() function
  2. NumPy argsort() function
  3. NumPy lexsort() function

So, let us begin!

1. NumPy sort() function

In order to sort the various elements present in the array structure, NumPy provides us with sort() function. With sort() function, we can sort the elements and segregate them in ascending to descending order, respectively.

Have a look at the below syntax!

Syntax:

numpy.sort(array, axis)

The parameter ‘axis’ specifies the manner in which sorting needs to be performed. So when we set axis = NONE, the sorting occurs in the traditional fashion, and the resultant array is a single row of elements. On the other hand, if we set axis = 1, the sorting occurs in a row-wise fashion, that is each and every row gets sorted individually.

Example 1:

In this example, we have created an array, further we have sorted the array using the sort() function and with axis = NONE i.e. it sorts the elements in ascending order.

import numpy as np
data = np.array([[22, 55], [0, 10]])
res = np.sort(data, axis = None)        
print ("Data before sorting:", data)
print("Data after sorting:", res)

Output:

Data before sorting: [[22 55]
 [ 0 10]]
Data after sorting: [ 0 10 22 55]

Example 2:

In this example, we have created an array and sorted the same using sort() function, here we have set axis = 1 i.e. row wise sorting has been performed.

import numpy as np
data = np.array([[66, 55, 22], [0, 10, -1]])
res = np.sort(data, axis = 1)        
print ("Data before sorting:", data)
print("Row wise sorting:", res)

Output:

Data before sorting: [[66 55 22]
 [ 0 10 -1]]
Row wise sorting: [[22 55 66]
 [-1  0 10]]

2. NumPy argsort()

Apart from the sort() method, we also have argsort() function that is used as a sorting techniques in NumPy which returns an array of indices of the sorted elements. From those sorted index values, we can get the sorted array elements in ascending order.

Thus, with argsort() function, we can sort the array values and get the index values of the same as a separate array.

Example:

import numpy as np
data = np.array([66, 55, 22,11,-1,0,10])
res_index = np.argsort(data)        
print ("Data before sorting:", data)
print("Sorted index values of the array:", res_index)

x = np.zeros(len(res_index), dtype = int)
for i in range(0, len(x)):
    x[i]= data[res_index[i]]
print('Sorted array from indexes:', x)

Output:

In the above example, we have performed argsort() function on the data values and have obtained sorted index values of the elements. Further, we have utilized the same array index values to get the sorted array elements.

Data before sorting: [66 55 22 11 -1  0 10]
Sorted index values of the array: [4 5 6 3 2 1 0]
Sorted array from indexes: [-1  0 10 11 22 55 66]

3. NumPy lexsort() function

The lexsort() function enables us to sort the data values using sequence of keys i.e. by columns. With lexsort() function, we sort the two arrays taking one at a time into consideration. As a result, we get the index values of the sorted elements.

import numpy as np
data = np.array([66, 55, 22,11,-1,0,10])
data1 = np.array([1,2,3,4,5,0,-1])
res_index = np.lexsort((data1, data))        
print("Sorted index values of the array:", res_index)

Output:

Sorted index values of the array: [4 5 6 3 2 1 0]

Conclusion

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!!

Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189
5 Techniques to Search NumPy array
Previous Post 5 Techniques to Search NumPy array
Next Post Python Calendar module - 6 IMP functions to know!
Python Calendar module – 6 IMP functions to know!

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
What Does %s Mean in Python String Formatting?

What Does %s Mean in a Python Format String?

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.