[R] Sorting based a custom sorting function

Martin Møller Skarbiniks Pedersen tr@xp|@yer @end|ng |rom gm@||@com
Thu Dec 14 12:38:18 CET 2023


On Thu, 14 Dec 2023 at 12:02, Duncan Murdoch <murdoch.duncan using gmail.com> wrote:
>

> class(df$value) <- "sizeclass"
>
> `>.sizeclass` <- function(left, right) custom_sort(unclass(left),
> unclass(right)) == 1
>
> `==.sizeclass` <- function(left, right) custom_sort(unclass(left),
> unclass(right)) == 0
>
> `[.sizeclass` <- function(x, i) structure(unclass(x)[i], class="sizeclass")
>
> df[order(df$value),]
>
> All the "unclass()" calls are needed to avoid infinite recursion.  For a
> more complex kind of object where you are extracting attributes to
> compare, you probably wouldn't need so many of those.

Great! Just what I need. I will create a class and overwrite > and ==.
I didn't know that order() used these exact methods.

My best solution was something like this:

quicksort <- function(arr, compare_func) {
  if (length(arr) <= 1) {
    return(arr)
  } else {
    pivot <- arr[[1]]
    less <- arr[-1][compare_func(arr[-1], pivot) <= 0]
    greater <- arr[-1][compare_func(arr[-1], pivot) > 0]
    return(c(quicksort(less, compare_func), pivot, quicksort(greater,
compare_func)))
  }
}

persons <- c("alfa", "bravo", "charlie", "delta", "echo", "foxtrot", "golf",
             "hotel", "india", "juliett", "kilo", "lima", "mike", "november",
             "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform",
             "victor", "whiskey", "x-ray", "yankee", "zulu")

quicksort(persons, function(left, right) {
  nchar(left) - nchar(right)
})

Regards
Martin



More information about the R-help mailing list