似乎是一个非常基本的问题,但我真的想不出一个“简单”的方法来做到这一点。
我想对包含具有基本 R 功能的语义版本号character
的向量进行排序:
vsns <- c("1", "10", "1.1", "1.10", "1.2", "1.1.1",
"1.1.10", "1.1.2", "1.1.1.1", "1.1.1.10", "1.1.1.2")
排序后应该是这样的:
# [1] "1" "1.1" "1.1.1" "1.1.1.1" "1.1.1.2" "1.1.1.10"
# [7] "1.1.2" "1.1.10" "1.2" "1.10" "10"
当然,这并没有让我得到我想要的,因为 R 只是按字母顺序对整个内容进行排序:
sort(vsns)
# [1] "1" "1.1" "1.1.1" "1.1.1.1" "1.1.1.10" "1.1.1.2" "1.1.10"
# [8] "1.1.2" "1.10" "1.2" "10"
vsns[order(vsns)]
# [1] "1" "1.1" "1.1.1" "1.1.1.1" "1.1.1.10" "1.1.1.2" "1.1.10"
# [8] "1.1.2" "1.10" "1.2" "10"
尝试对其进行规范化(有点沿着这篇文章),但我想不出适合语义版本结构的匹配/替换方案:
tmp <- gsub("\\.", "", vsns)
# [1] "011" "021" "0101" "0201"
tmp_nchar <- sapply(tmp, nchar)
to_add <- max(tmp_nchar) - tmp_nchar
tmp <- sapply(1:length(tmp), function(ii) {
paste0(tmp[ii], paste(rep("A", to_add[ii]), collapse = ""))
})
# [1] "10" "1.10" "1.1.10" "1.1.1.10" "1.1.1.1" "1.1.1.2" "1.1.1"
# [8] "1.1.2" "1.1" "1.2" "1"
vsns[order(tmp)]
# [1] "1AAAA" "10AAA" "11AAA" "110AA" "12AAA" "111AA" "1110A" "112AA" "1111A" "11110"
# [11] "1112A"
到目前为止我能想到的最好的就是这个,但它看起来很漂亮......参与;-)
sortVersionNumbers <- function(x, decreasing = FALSE) {
tmp <- strsplit(x, split = "\\.")
tmp_l <- sapply(tmp, length)
idx_max <- which.max(tmp_l)[1]
tmp_l_max <- tmp_l[idx_max]
tmp_n <- lapply(tmp, function(ii) {
ii_l <- length(ii)
if (ii_l < tmp_l_max) {
c(ii, rep(NA, (tmp_l_max - ii_l)))
} else {
ii
}
})
tmp <- matrix(as.numeric(unlist(tmp_n)), nrow = length(tmp_n), byrow = TRUE)
tmp_cols <- ncol(tmp)
expr <- paste0("order(", paste(paste0("tmp[,", 1:tmp_cols, "]"),
collapse = ", "), ", na.last = FALSE",
ifelse(decreasing, ", decreasing = FALSE)", ")"))
idx <- eval(parse(text = expr))
tmp_2 <- tmp[idx,]
sapply(1:nrow(tmp_2), function(ii) {
paste(na.omit(tmp_2[ii,]), collapse = ".")
})
}
sortVersionNumbers(vsns)
# [1] "1" "1.1" "1.1.1" "1.1.1.1" "1.1.1.2" "1.1.1.10" "1.1.2"
# [8] "1.1.10" "1.2" "1.10" "10"
sortVersionNumbers(sort(vsns))
# [1] "1" "1.1" "1.1.1" "1.1.1.1" "1.1.1.2" "1.1.1.10" "1.1.2"
# [8] "1.1.10" "1.2" "1.10" "10"