问题
Seems like a pretty basic question, but I can't really figure out an "easy" way to do it.
I'd like to sort a character
vector containing semantic version numbers with base R functionality:
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")
It should look like this after sorting:
# [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"
This doesn't get me what I want, of cours, as R simply sorts the whole thing alphabetically:
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"
Trying normalizing it (somewhat along this post), but I can't think of a matching/substitution scheme that would fit the structure of semantic versions:
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"
The best I could come up with so far is this, but it seems pretty... Involved ;-)
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"
回答1:
From ?numeric_version
> sort(numeric_version(vsns))
[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'
It's relatively interesting to see how this is implemented. numeric_version
splits a single version string into integer parts, and stores the vector of versions as a list of integer vectors. A method on xtfrm
(which is used by sort()
) transforms the vector of integers making up each version string into a numeric value, with the guts being
base <- max(unlist(x), 0, na.rm = TRUE) + 1
x <- vapply(x, function(t) sum(t/base^seq.int(0, length.out = length(t))),
1)
the result is a numeric vector that can be used to order the original vector in a standard way. Thus an ad hoc solution is
xtfrm.my_version <- function(x) {
x <- lapply(strsplit(x, ".", fixed=TRUE), as.integer)
base <- max(unlist(x), 0, na.rm = TRUE) + 1
vapply(x, function(t) sum(t/base^seq.int(0, length.out = length(t))), 1)
}
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")
class(vsns) = "my_version"
sort(vsns)
回答2:
Does this work
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")
x <- strsplit(vsns, "\\.")
max.length <- max(sapply(x, function(i) max(nchar(i))))
y <- lapply(x, function(i) sprintf(as.numeric(i), fmt = paste0("%0", max.length, "d")))
y <- sapply(y, paste, collapse = ".")
vsns[order(y)]
# [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"
回答3:
Try:
ll = strsplit(vsns,'\\.')
dd = data.frame(t(sapply(ll, c)))
dd = data.frame(apply(dd, 2, function(x) as.numeric(as.character(x))))
dd = with(dd, dd[order(X1,X2,X3),])
ans = apply(dd, 1, paste, collapse=".")
ans
1 2 3 4
"0.1.1" "0.2.1" "0.10.1" "0.20.1"
回答4:
Try, with new vsns data:
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")
dd = data.frame(vsns)
library(splitstackshape)
dd2 = concat.split.expanded(dd, 'vsns', '.', fill = 0, drop = TRUE)
dd3 = cbind(dd, dd2)
dd4= with(dd3, dd3[order(vsns_1, vsns_2, vsns_3, vsns_4),])
dd4[is.na(dd4)]=0
dd4
vsns vsns_1 vsns_2 vsns_3 vsns_4
9 1.1.1.1 1 1 1 1
11 1.1.1.2 1 1 1 2
10 1.1.1.10 1 1 1 10
6 1.1.1 1 1 1 0
8 1.1.2 1 1 2 0
7 1.1.10 1 1 10 0
3 1.1 1 1 0 0
5 1.2 1 2 0 0
4 1.10 1 10 0 0
1 1 1 0 0 0
2 10 10 0 0 0
>
apply(dd4[,2:5], 1, paste, collapse='.')
9 11 10 6 8 7 3 5 4 1
" 1.1.1.1" " 1.1.1.2" " 1.1.1.10" " 1.1.1.0" " 1.1.2.0" " 1.1.10.0" " 1.1.0.0" " 1.2.0.0" " 1.10.0.0" " 1.0.0.0"
2
"10.0.0.0"
回答5:
Here's a solution that generalizes to version numbers with different numbers of blocks (the indented sapply + ifelse
lines) and can handle mixed numbers and letters (the mixedsort
line).
library(gtools)
vsns <- c("0.1.1", "0.10", "0.2.1", "0.2.1a", "0.20", "0.20.1.3")
v <- strsplit(vsns, "\\.")
tmp <- data.frame(sapply(1:max(sapply(v, length)), function(i){
vv <- sapply(v, "[", i)
ifelse(is.na(vv), "0", vv)
}), stringsAsFactors=FALSE)
vsns[do.call(mixedorder, tmp)]
[1] "0.1.1" "0.2.1" "0.2.1a" "0.10" "0.20" "0.20.1.3"
来源:https://stackoverflow.com/questions/26034308/sorting-character-vector-containing-semantic-versions