2

So I've a 2d array, that when sorted by the second column using a[np.argsort(-a[:,1])] looks like this:

array([[ 30.        ,  98.7804878 ],
       [ 24.        ,  98.7804878 ],
       [ 21.        ,  98.7804878 ],
       [ 26.        ,  98.7804878 ],
       [ 20.        ,  98.70875179],
       [  4.        ,  98.27833572],
       [  1.        ,   7.10186514]])

Now I want to sort this by the lowest "id" column so it looks like this:

array([[ 21.        ,  98.7804878 ],
       [ 24.        ,  98.7804878 ],
       [ 26.        ,  98.7804878 ],
       [ 30.        ,  98.7804878 ],
       [ 20.        ,  98.70875179],
       [  4.        ,  98.27833572],
       [  1.        ,   7.10186514]])

I can't figure out how to do it, even if I take the top highest percentages from the first and then order them.

Alex Riley
  • 152,205
  • 43
  • 245
  • 225
Blease
  • 1,274
  • 3
  • 37
  • 61

1 Answers1

7

You can use np.lexsort for this:

>>> a[np.lexsort((a[:, 0], -a[:, 1]))]
array([[ 21.        ,  98.7804878 ],
       [ 24.        ,  98.7804878 ],
       [ 26.        ,  98.7804878 ],
       [ 30.        ,  98.7804878 ],
       [ 20.        ,  98.70875179],
       [  4.        ,  98.27833572],
       [  1.        ,   7.10186514]])

This sorts by -a[:, 1], then by a[:, 0], returning an array of indices than you can use to index a.

Alex Riley
  • 152,205
  • 43
  • 245
  • 225