tags:

views:

124

answers:

1

Is there an alphanumeric sort for R?

Say I had a character vector like so:

> seq.names <- c('abc21', 'abc2', 'abc1', 'abc01', 'abc4', 'abc201', '1b', '1a')

I'd like to sort it aphanumerically, so I get back this:

c('1a', '1b', 'abc1', 'abc01', 'abc2', 'abc4', 'abc21', 'abc201')

Does this exist somewhere, or should I start coding? Thanks,

-chris

+11  A: 

I don't think "alphanumeric sort" means what you think it means.

In any case, looks like you want mixedsort.

> install.packages('gtools')
[...]
> require('gtools')
Loading required package: gtools
> n
[1] "abc21"  "abc2"   "abc1"   "abc01"  "abc4"   "abc201" "1b"     "1a"    
> mixedsort(n)
[1] "1a"     "1b"     "abc1"   "abc01"  "abc2"   "abc4"   "abc21"  "abc201"
Nicholas Riley
Excellent! Is alphanumeric sort not this right term for this? Have I been calling it the wrong thing all along?
cbare
Alphanumeric sort would like that what is returned from the R sort() function. Each character is evaluated based on ASCII value of the position. Smaller values are sorted first. In this case, "abc01" would be before "abc1" because ASCII value "0" (48) is smaller than "1" (49) for position 4.
beach
I've typically used the term "natural order sort" after one of the first widely used pieces of software to do this (http://www.naturalordersort.org/). Jeff Atwood even wrote an blog post about it (http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html).
Nicholas Riley