6

I try to sort an array:

import numpy as np

arr = [5,3,7,2,6,34,46,344,545,32,5,22]
print "unsorted"
print arr

np.argsort(arr)

print "sorted"
print arr

But the output is:

unsorted
[5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]
sorted
[5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]

The array does not change at all

2
  • 5
    Such things are searchable in 1 minute in docs: docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html 1. It returns indexes of sorted elements. 2. It doesn't do it in place, but returns new array. Commented Nov 6, 2013 at 4:26
  • 1
    Use arr.sort() for in-place sort. Commented Nov 6, 2013 at 5:38

4 Answers 4

25

np.argsort doesn't sort the list in place, it returns a list full of indicies that you are able to use to sort the list.

You must assign this returned list to a value:

new_arr = np.argsort(arr)

Then, to sort the list with such indices, you can do:

np.array(arr)[new_arr]
Sign up to request clarification or add additional context in comments.

4 Comments

I think you mean indices = np.argsort(arr); arr[indices]. Note that argsort is not sort
@askewchan It's simply a name. OP can name it whatever he wishes :)
True, sorry, I meant your first sentence, "np.argsort doesn't sort the list in place, it returns a sorted list" It returns the indices which would sort a list. Look at what the OP example returns: array([ 3, 1, 0, 10, 4, 2, 11, 9, 5, 6, 7, 8]), which i would not call sorted.
@askewchan Thank you. I have never fully working with numpy, and so I will edit my answer
8

Try

order = np.argsort(arr)
print np.array(arr)[order]

the argsort response is the index of the elements.

Comments

7

There are two issues here; one is that np.argsort returns an array of the indices which would sort the original array, the second is that it doesn't modify the original array, just gives you another. This interactive session should help explain:

In [59]: arr = [5,3,7,2,6,34,46,344,545,32,5,22]

In [60]: np.argsort(arr)
Out[60]: array([ 3,  1,  0, 10,  4,  2, 11,  9,  5,  6,  7,  8])

Above, the [3, 1, 0, ...] means that item 3 in your original list should come first (the 2), then item 2 should come (the 3), then the first (index is 0, item is 5) and so on. Note that arr is still unaffected:

In [61]: arr
Out[61]: [5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]

You might not need this array of indices, and would find it easier to just use np.sort:

In [62]: np.sort(arr)
Out[62]: array([  2,   3,   5,   5,   6,   7,  22,  32,  34,  46, 344, 545])

But this still leaves arr alone:

In [68]: arr
Out[68]: [5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]

If you want to do it in place (modify the original), use:

In [69]: arr.sort()

In [70]: arr
Out[70]: [2, 3, 5, 5, 6, 7, 22, 32, 34, 46, 344, 545]

2 Comments

Just as a note, np.sort(arr) returns a copy of the sorted array rather than sorting in-place
@Josh, True, that's what arr.sort() is for, at the end, since np.sort(arr) leaves arr itself alone.
2

If you want your array sorted in-place you want arr.sort():

In [1]: import numpy as np  
In [2]: arr = [5,3,7,2,6,34,46,344,545,32,5,22]

In [4]: print arr
[5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]

In [5]: arr.sort()
In [7]: print arr
[2, 3, 5, 5, 6, 7, 22, 32, 34, 46, 344, 545]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.