Sorting two numpy arrays

Suppose you have two numpy arrays arr1 and arr2 and you want to sort them in such a way that one of them is sorted with the index used to sort the second array. Probably not clear at this point. So, let's just look at an example:

In [1]: arr1 = array([3,1,4,2])
In [2]: arr2 = array([5,3,1,6])
In [3]: ind = lexsort((arr1,arr2))
In [4]: print arr1[ind]
[4 1 3 2]
In [4]: print arr2[ind]
[1 3 5 6]

Doing this, we have sorted the two arrays using the sorting order of the second one. For using sorting order of first array, simply inverse them in lexsort(). Note: in this case, all you're trying to do is getting lexsort() to return the indices of the sorting. So, it doesn't matter what you put as a first array.

Comments

Popular Posts