Learn R Programming

funprog (version 0.3.0)

sort_by: Sort with auxiliary function

Description

Sort a vector or a list, given one or more auxiliary functions.

Usage

sort_by(x, ..., method = c("auto", "shell", "radix"))

Arguments

x

vector or list to sort.

...

one or several functions to apply to x. Use descending for reversed order.

method

the method for ties (see order).

Value

A vector or list containing rearranged elements of x.

Details

The output of the first function will be used as first key for sorting, the output of the second function as second key, and so on... Therefore, these outputs should be sortable (i.e. atomic vectors).

sort_by is inspired by sortBy in Haskell.

See Also

order which is used for rearranging elements.

Examples

Run this code
# NOT RUN {
sort_by(-3:2, abs)
sort_by(-3:2, abs, function(x) -x)
sort_by(list(5:7, 0, 1:4), length)
sort_by(list(1:2, 3:4, 5), length, descending(sum))
# }

Run the code above in your browser using DataLab