Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting character vector containing semantic versions

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" 
like image 515
Rappster Avatar asked Dec 11 '22 03:12

Rappster


1 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 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)
like image 112
Martin Morgan Avatar answered Jan 10 '23 10:01

Martin Morgan