jm96
jm96

Reputation: 57

Order rows of binary values

I have some numpy arrays that looks like this:

[[0,0,1],
 [0,1,0],
 [0,0,0]]

[[0,0,1],
 [0,0,0],
 [0,1,0]]

I want to order the rows so that every time I get a different array, it is ordered consistently. So for the two arrays shown above, I want to get the same result:

[[0,0,0],
 [0,0,1],
 [0,1,0]]

I thought about doing it as a data frame merging the columns and then sorting but I am not sure if this is the best way to do it.

Upvotes: 1

Views: 167

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114468

You can use np.lexsort:

a[np.lexsort(a.T[::-1])]

Upvotes: 5

Related Questions