On Thu, 14 Dec 2023 at 12:02, Duncan Murdoch <murdoch.dun...@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 ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.