How to perform natural sorting?

笑着哭i 提交于 2019-11-26 07:42:07

问题


Is there a natural 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?


回答1:


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"


来源:https://stackoverflow.com/questions/2778039/how-to-perform-natural-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!