Search code examples
rsortingarbitrary-precision

How can I sort a vector of bigz integers in R?


I am working in R using arbitrary precision arithmetic in the gmp package. This package creates and stores large integers in the bigz form. For example, you can create a vector of arbitrarily large integers as follows:

X <- as.bigz(c("734876349856913169345", "610034193791098", "82348779011105371828395319",
               "810367198176345917234", "92573840155289", "729811850143511981", "51385",
               "358934723", "751938475", "72265018270590", "12838756105612376401932875"));

I would like to sort this vector of large integers (smallest to largest). Although the documentation for bigz objects notes that they can be compared with inequality operations, unfortunately the standard sort function does not work on them:

sort(X)
Error in rank(x, ties.method = "min", na.last = "keep") : 
  raw vectors cannot be sorted

Question: How can I take a bigz vector like the one above and sort it in ascending order?


Solution

  • It involves coercing to string and back but you could possibly use str_sort(). The argument numeric = TRUE gives a natural rather than alphanumeric sort order.

    library(stringr)
    library(gmp)
    
    as.bigz(str_sort(BIGINTEGERS, numeric = TRUE))
    Big Integer ('bigz') object of length 11:
     [1] 51385                      358934723                  751938475                  72265018270590             92573840155289             610034193791098           
     [7] 729811850143511981         734876349856913169345      810367198176345917234      12838756105612376401932875 82348779011105371828395319