Skip to main content

NumPy: Sorting Arrays Using lexsort()

It is used to perform indirect sorting using a sequence of keys.

If multiple sorting keys can be interpreted as columns in a spreadsheet. Here, we can use lexsort() which returns an array of integer indices that describe the sorted order of multiple columns.
In this method, the last key in the sequence is used as the primary sorting order and the second-to-last key for secondary sort order and so on.


numpy.lexsort(keys, axis=- 1)


marks= np.array([10,15,10,14,13,14]) # First column
n= np.array(['b','d','a','d','c','f']) # Second column
rank= np.lexsort((b,a)) # Sort by a, then by b


[2 0 4 3 5 1]
Submitted by devanshi.srivastava on September 7, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.