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.
represents an operator form of ReverseSortBy that can be applied to an expression.
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.
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 all close allBasic Examples (5)
Reverse sort a list of lists by the last element of each sublist:
ReverseSortBy[{{1, 3}, {2, 1}, {2, 2}, {3, 2}}, Last]Reverse sort by the total of each sublist:
ReverseSortBy[{{1, 3}, {2, 1}, {2, 2}, {3, 2}}, Total]Reverse sort a list of integers by their values modulo 3:
ReverseSortBy[Range[10], Mod[#, 3]&]Reverse sort elements of an Association by their values modulo 3:
ReverseSortBy[<|2 -> 1, 3 -> 2, 1 -> 3|>, Mod[#, 3]&]Use ReverseSortBy as an operator form:
ReverseSortBy[Last][{{1, 3}, {2, 1}, {2, 2}, {3, 2}}]Scope (6)
Reverse sort by numerical value:
ReverseSortBy[{E, Pi, Sin[1], Sqrt[2]}, N]The default reverse order ranks these symbolically:
ReverseSort[{E, Pi, Sin[1], Sqrt[2]}]Reverse sort by absolute numerical value:
ReverseSortBy[{1., Sqrt[1 + Exp[-10]], Exp[2 + I], Exp[2 I - 1], Exp[I]}, N[Abs[#]]&]Reverse sort by Re, Im, Arg and Abs:
data = RandomComplex[{-1 - I, 1 + I}, 20];listOrderPlot[data_] := Graphics[Map[Arrow, Partition[Map[{Re[#], Im[#]}&, data], 2, 1]], PlotRange -> 1, Frame -> True]Table[listOrderPlot@ReverseSortBy[data, f], {f, {Re, Im, Arg, Abs}}]Reverse sort dates by converting to their absolute time form:
dates = {"Mon 21 May 2007 08:20:36", "Sun 4 Mar 2007 04:42:29", "Thu 8 Mar 2007 18:21:40", "Thu 8 Mar 2007 18:08:36", "Sun 2 Jan 2005 07:50:25", "Wed 11 Mar 2009 08:10:14", "Thu 8 Mar 2007 22:02:12", "Thu 8 Mar 2007 18:07:24", "Thu 8 Mar 2007 18:08:43", "Fri 9 Mar 2007 08:54:39"};TableForm[ReverseSortBy[dates, AbsoluteTime]]Take a sample of rows from the Titanic resource data:
tab = RandomSample[ResourceData["Sample Tabular Data: Titanic"], 8]Reverse-sort them by age, placing Missing[…] values at then end:
ReverseSortBy[tab, Function[#age]]ReverseSortBy[tab, "age"]Reverse sort complex numbers by their modulus using numerical order:
ReverseSortBy[{Sqrt[1 + Exp[-10I]], Exp[2 + I], Exp[2 I - 1], Exp[I]}, Abs, NumericalOrder]Abs[%]//NUsing canonical order gives a different result:
ReverseSortBy[{Sqrt[1 + Exp[-10I]], Exp[2 + I], Exp[2 I - 1], Exp[I]}, Abs]Abs[%]//NReverse sort by string length:
ReverseSortBy[{"dog", "zebra", "donkey", "anteater", "fish"}, StringLength]Reverse sort the strings using Greater as the ordering function:
ReverseSortBy[{"dog", "zebra", "donkey", "anteater", "fish"}, StringLength, Greater]Generalizations & Extensions (1)
ReverseSortBy can be used on expressions with any head:
ReverseSortBy[g[h[1, 2], h[1, 1], h[2, 2], h[1]], Last]Applications (1)
Reverse sort mathematically equivalent expressions by different criteria:
e = Sin[2 x] / (1 - Cos[x] ^ 2) + Cos[2 x] / (1 - Sin[x] ^ 2);
ops = {Identity, Together, TrigReduce, TrigExpand, Simplify, FullSimplify};
exprs = Table[op[e], {op, ops}];TableForm[Transpose[{ops, exprs}]]Reverse sort by LeafCount:
ReverseSortBy[exprs, LeafCount]Reverse sort by size of error for numerical evaluation with machine numbers at
:
hp = N[e /. x -> 1, 100];
err[e_] := Abs[hp - N[e /. x -> 1.]];ReverseSortBy[exprs, err]Properties & Relations (1)
ReverseSortBy[list,f] is equivalent to Reverse[SortBy[list,f]], even in the presence of ties:
list = Range[-5, 5]ReverseSortBy[list, Abs]% === Reverse[SortBy[list, Abs]]Use ReverseSortBy[list,{f}] to avoid reordering ties:
ReverseSortBy[list, {Abs}]Related Guides
History
Text
Wolfram Research (2019), ReverseSortBy, Wolfram Language function, https://reference.wolfram.com/language/ref/ReverseSortBy.html.
CMS
Wolfram Language. 2019. "ReverseSortBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ReverseSortBy.html.
APA
Wolfram Language. (2019). ReverseSortBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReverseSortBy.html
BibTeX
@misc{reference.wolfram_2026_reversesortby, author="Wolfram Research", title="{ReverseSortBy}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/ReverseSortBy.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_reversesortby, organization={Wolfram Research}, title={ReverseSortBy}, year={2019}, url={https://reference.wolfram.com/language/ref/ReverseSortBy.html}, note=[Accessed: 13-June-2026]}