Sorting a character vector containing semantic versions

This seems like a pretty simple question, but I can't figure out how to do it "easily".

I would like to sort a vector character

containing semantic version numbers with R base 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")

      

After sorting, it should look like this:

# [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 give me what I want from the course, as R just sorts everything 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 to normalize it (somewhat on this post), but I can't think of a matching / substitution scheme that matches the structure of the 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 think of 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" 

      

+3


source to share


5 answers


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 one version line into integer parts and stores the version vector as a list of integer vectors. The method xtfrm

(which is used sort()

) converts the vector of integers that make up each line of the version to a numeric value, with the guts



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 standardize the ordering of the original vector. So the 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)

      

+6


source


Does it 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"      

      

+2


source


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" 

      

+1


source


Try 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" 

      

+1


source


Here's a solution that generalizes version numbers with different block numbers (indentation sapply + ifelse

) and can handle mixed numbers and letters (string mixedsort

).

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"

      

0


source







All Articles