NumPy: Sorting Arrays Using lexsort()

Profile picture for user devanshi.srivastava
Submitted by devanshi.srivastava on

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]