Lexsort supports the ordering of arrays in the order of specified rows or columns; is an indirect sort, lexsort does not modify the original array and returns the index. By default, the last line element has a small to large sort, which returns the position of the last row of elements after the index is sorted. Set array A, return index IND, a can be a 1-D or 2-dimensional array, IND returns a one-dimensional array for a one-dimensional array, A[ind] is the sorted array. For a two-dimensional array, an example is detailed below. Import NumPy as NP >>> Aarray ([[2, 7, 4, 2], [35, 9, 1, 5], [22, 12, 3, 2]])
sort by the last column order>>> A[np.lexsort (A.T)]array ([[22, 12, 3, 2], [2, 7, 4, 2], [35, 9, 1, 5]])
sort by the last column in reverse order>>>a[np.lexsort (-A.T)]array ([[35, 9, 1, 5], [2, 7, 4, 2], [22, 12, 3, 2]])
Sort by first column order>>> A[np.lexsort (A[:,::-1]. T)]array ([[2, 7, 4, 2], [22, 12, 3, 2], [35, 9, 1, 5]])
sort by last line order>>> A.t[np.lexsort (a)]. Tarray ([[2, 4, 7, 2], [5, 1, 9, 35], [2, 3, 12, 22]])
Sort by first row order>>> A.t[np.lexsort (a[::-1,:])]. Tarray ([[2, 2, 4, 7], [5, 35, 1, 9], [2, 22, 3, 12]])
Python sort by a row or column of a two-dimensional array (NumPy lexsort)