1

If I want to sort this array which is declared by np.zeros((50),dtype=object) and I want to sort this array by first value,

[list([2, 5]) list([3, 0]) list([2, 7]) list([3, 1]) list([11, 2])]

like

[list([2, 5]) list([2, 7]) list([3, 0]) list([3, 1]) list([11, 2])]

What should I use? I've been try sort(), sorted() etc..

Ehsan
  • 12,072
  • 2
  • 20
  • 33
Harry Lin
  • 13
  • 3

1 Answers1

0

if all lists in your array are of the same length, you can use numpy lexsort (a is your array):

a = np.array(a)
a[np.lexsort((a[:,-1],a[:,-2]))]
#[[ 2  5]
# [ 2  7]
# [ 3  0]
# [ 3  1]
# [11  2]]

or equivalently:

a[np.lexsort(a.T[::-1,:])]
Ehsan
  • 12,072
  • 2
  • 20
  • 33