1

I have a matrix with 10 columns of different types. I sorted them based on the alphanumeric column with :

data = np.sort(data, axis=0,order='AlphaNumColumn')

It didn't do the job right, i.e.

BFT_job10_q0
BFT_job13_q0
BFT_job13_q1
BFT_job1_q0

instead of :

BFT_job1_q0
BFT_job10_q0
BFT_job13_q0
BFT_job13_q1

Anything numpy could do about it?? Thanks!

1

1 Answer 1

3

The sorting order seems to be right. I would recommend you to review your numbering:

1 becomes 01

If you have to keep your numbering, you can also do:

key = lambda x: '.'.join(x.split('_')[1:3]).replace('job','').replace('q','')

a[np.argsort([float(key(i)) for i in a[:,0]])]

Where key() will do the following:

key('BFT_job10_q0') --> 10.
key('BFT_job1_q0')  --> 1.
key('BFT_job13_q1') --> 13.1
key('BFT_job13_q0') --> 13.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, nicely thought through! Problem is, I have to keep the names as they are, as it is important for downstream work... So I could use key() after sorting to place the removed name parts back in place -and I'll do that, so thanks again-, but is there any way we could easily get an alphanumeric sort?
Don't worry. the names are kept as they are. I just showed you how key() is working. But this function is used only for sorting, without changing the original values...
Hi, again. I feel helpless. I've tried to adapt your code to a new set of names, but I can't seem to find the way to do it. Data is typically the same as before, but this time, there's a little more: BFT_job10_q0_type0 etc. Your code doesn't work as a number with 2 decimals is not a float anymore...

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.