我有一个清单:
a <- ["12file.txt", "8file.txt", "66file.txt"]
我想按数字排序:
a would be: ["8file.txt", "12file.txt", "66file.txt"]
现在我只能得到这个:
a = ["12file.txt", "66file.txt", "8file.txt"]
谢谢
我有一个清单:
a <- ["12file.txt", "8file.txt", "66file.txt"]
我想按数字排序:
a would be: ["8file.txt", "12file.txt", "66file.txt"]
现在我只能得到这个:
a = ["12file.txt", "66file.txt", "8file.txt"]
谢谢
我假设你有一个字符向量:
a <- c("12file.txt", "8file.txt", "66file.txt")
我会通过提取每个字符串开头的数字并对其进行排序来解决这个问题:
num <- as.numeric(sub("([0-9]+).*", "\\1", a))
a[order(num)]
#[1] "8file.txt" "12file.txt" "66file.txt"
您还可以通过设置字段长度来用空格填充字符串sprintf
以实现所需的排序:
a[order(sprintf("%10s",a))]
[1] "8file.txt" "12file.txt" "66file.txt"