x <- c("a1", "a100", "a101", "a1000", "a10", "a10", "a11", "a99", "a10", "a1")
base::sort.default(x) # lexicographic sort
sort(x, numeric=TRUE) # calls stringx:::sort.character
xtfrm2(x, numeric=TRUE) # calls stringx:::xtfrm2.character
rank(xtfrm2(x, numeric=TRUE), ties.method="average") # ranks with averaged ties
order(xtfrm2(x, numeric=TRUE)) # ordering permutation
x[order(xtfrm2(x, numeric=TRUE))] # equivalent to sort()
# order a data frame w.r.t. decreasing ids and increasing vals
d <- data.frame(vals=round(runif(length(x)), 1), ids=x)
d[order(-xtfrm2(d[["ids"]], numeric=TRUE), d[["vals"]]), ]
Run the code above in your browser using DataLab