我只是在 gtools::mixedsort
中发现了一些意想不到的行为看看我的输出是如何自然排序的。
我有一个这样的例子:
aa=c("CD57","CD58","CD158","CD158b","CD158e","CD158e1","CD319","CD335")
gtools::mixedsort(aa)
我的预期结果是:[1] "CD57" "CD58" "CD158" "CD158b" "CD158e" "CD158e1" "CD319"
[8] "CD335"
但是我得到了这个:[1] "CD57" "CD58" "CD158" "CD158b" "CD158e" "CD319" "CD335"
[8] "CD158e1"
这样对吗?是什么原因?
最佳答案
CD158e1
被视为 1580
在这里,因为:
>>> 158e1
1580.0
>>>
158e1
是一个带有 e
的欧拉数,所以它多给一个 0
, e2
会给2个额外的等等......所以这就是为什么它被解析为列表中的最后一个。
如 the documentation of
mixedsort
中所述:These functions sort or order character strings containing embedded numbers so that the numbers are numerically sorted rather than sorted by character value. I.e. "Aspirin 50mg" will come before "Aspirin 100mg". In addition, case of character strings is ignored so that "a", will come before "B" and "C".
关于R:gtools混合排序的意外自然排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69388929/