ReverseSortBy

ReverseSortBy[list,f]

sorts the elements of list using the reverse canonical order defined by applying f to each of them.

ReverseSortBy[list,f,p]

sorts the elements of list using the function p to compare pairs of results of applying f to each element.

ReverseSortBy[f]

represents an operator form of ReverseSortBy that can be applied to an expression.

Details

  • ReverseSortBy[{e1,e2,e3,},f] sorts the ei so that the f[ei] lie in reverse canonical order.
  • If some of the f[ei] are the same, then the reverse canonical order of the corresponding ei is used.
  • ReverseSortBy[list,f] is equivalent to reversing the result of SortBy[list,f].
  • In the result of ReverseSortBy[{e1,e2,e3,},f,p], element ei will be sorted before ej if p[f[ei],f[ej]] is -1 or False.
  • ReverseSortBy[list,{f1,f2,}] breaks ties by successively using the values obtained from the fi.
  • ReverseSortBy can be used on expressions with any head, not only List.
  • ReverseSortBy[f][list] is equivalent to ReverseSortBy[list,f].

Examples

open allclose all

Basic Examples  (5)

Reverse sort a list of lists by the last element of each sublist:

Reverse sort by the total of each sublist:

Reverse sort a list of integers by their values modulo 3:

Reverse sort elements of an Association by their values modulo 3:

Use ReverseSortBy as an operator form:

Scope  (5)

Reverse sort by numerical value:

The default reverse order ranks these symbolically:

Reverse sort by absolute numerical value:

Reverse sort by Re, Im, Arg and Abs:

Reverse sort dates by converting to their absolute time form:

Reverse sort complex numbers by their modulus using numerical order:

Using canonical order gives a different result:

Reverse sort by string length:

Reverse sort the strings using Greater as the ordering function:

Generalizations & Extensions  (1)

ReverseSortBy can be used on expressions with any head:

Applications  (1)

Reverse sort mathematically equivalent expressions by different criteria:

Reverse sort by LeafCount:

Reverse sort by size of error for numerical evaluation with machine numbers at :

Properties & Relations  (1)

ReverseSortBy[list,f] is equivalent to Reverse[SortBy[list,f]], even in the presence of ties:

Use ReverseSortBy[list,{f}] to avoid reordering ties:

Introduced in 2019
 (12.0)