Issue
I want to sort 2d array column-wise consequently, so if the values in one column are equal then sorting is performed by next column. For example array
[[1, 0, 4, 2, 3]
[0, 1, 5, 7, 4]
[0, 0, 6, 1, 0]]
must be sorted as
[[0, 0, 6, 1, 0]
[0, 1, 5, 7, 4]
[1, 0, 4, 2, 3]]
So rows must not be changed, only their order. How can I do that?
Solution
This should work
import numpy as np
a = np.array([[1, 0, 4, 2, 3],[0, 1, 5, 7, 4],[0, 0, 6, 1, 0]])
np.sort(a.view('i8,i8,i8,i8,i8'), order=['f0'], axis=0).view(np.int)
I get
array([[0, 0, 6, 1, 0],
[0, 1, 5, 7, 4],
[1, 0, 4, 2, 3]])
f0
is the column which you want to sort by.
Answered By - anarchy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.