R/sort_based.R

Defines functions sort_based

Documented in sort_based

#' @title Sort the elements based on a results of a function called on them.
#'
#' @description Sort the elements based on a results of a function called on them.
#'
#' @param x A vector or list.
#' @param .f Called function.
#'
#' @examples
#' sort_based(c("a22", "b33", "c15"), parse_number)
#'
#' @export

sort_based <- function(x, .f) {
  order <- purrr::map_dbl(x, .f) |>
    rank() |>
    tibble::enframe(value = "rank") |>
    dplyr::arrange(rank) |>
    dplyr::pull(name)

  x[order]
}
MarcellGranat/granatlib documentation built on July 9, 2023, 6:08 a.m.