Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a 2D numpy array and I would like to sort the rows based on first column values. The trouble is the way it is formatted:

Column I am sorting by: 0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,etc ->you can see it repeats itself

Basically I want to group the 1s, then 2s, then 3s, then 4s. The ordering of the matching values matter: I want the 1st '1' row to be the first one that appeared in the unsorted array, followed by the one that shows up next, etc. I use this command: sortedData= myData[myData[:,0].argsort()]

Unfortunately, it doesn't not appear to order matching columns based on the original ordering of the array. Are there certain options I can turn on to enable this?

Thanks!

share|improve this question
up vote 2 down vote accepted

You can change the sorting algorithm used by argsort with the kind argument.

Use

sortedData= myData[myData[:,0].argsort(kind='mergesort')]

to preserve the order of the equal items. (Merge sort is a stable sorting algorithm.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.