SortBy[list,f]
sorts the elements of list in the order defined by applying f to each of them.
SortBy[list,{f1,f2,…}]
breaks ties by successively using the values obtained from the fi.
SortBy[list,f,p]
sorts the elements of list using the function p to compare the results of applying f to each element.
SortBy
SortBy[list,f]
sorts the elements of list in the order defined by applying f to each of them.
SortBy[list,{f1,f2,…}]
breaks ties by successively using the values obtained from the fi.
SortBy[list,f,p]
sorts the elements of list using the function p to compare the results of applying f to each element.
Details
- SortBy[{e1,e2,e3,…},f] sorts the ei so that the f[ei] lie in canonical order.
- If some of the f[ei] are the same, then the canonical order of the corresponding ei is used.
- SortBy[{e1,e2,e3,…},f,p] sorts the ei so that the f[ei] are sorted according to the ordering function p: ei will be sorted before ej if p[f[ei],f[ej]] is 1 or True.
- SortBy uses the canonical ordering described in the notes for Sort.
- SortBy can be used on expressions with any head, not only List.
- SortBy[f][list] is equivalent to SortBy[list,f].
Examples
open all close allBasic Examples (4)
Sort a list of lists by the last element of each sublist:
SortBy[{{1, 2, 3}, {2, 3, 1}, {3, 1, 2}, {2, 2}}, Last]Sort by the total of each sublist:
SortBy[{{1, 2, 3}, {2, 3, 1}, {3, 1, 2}, {2, 2}}, Total]Sort a list of integers by their values modulo 3:
SortBy[Range[10], Mod[#, 3]&]Sort elements of an Association by their values modulo 3:
SortBy[<|2 -> 1, 3 -> 2, 1 -> 3|>, Mod[#, 3]&]Use SortBy as an operator form:
SortBy[Last][{{1, 2, 3}, {2, 3, 1}, {3, 1, 2}, {2, 2}}]Scope (8)
SortBy[{E, Pi, Sin[1], Sqrt[2]}, N]The default order ranks these symbolically:
Sort[{E, Pi, Sin[1], Sqrt[2]}]Sort by absolute numerical value:
SortBy[{1., Sqrt[1 + Exp[-10]], Exp[2 + I], Exp[2 I - 1], Exp[I]}, N[Abs[#]]&]SortBy[{1., Sqrt[1 + Exp[-10]], Exp[2 + I], Exp[2 I - 1], Exp[I]}, RealExponent]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@SortBy[data, f], {f, {Re, Im, Arg, Abs}}]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[SortBy[dates, AbsoluteTime]]Take a sample of rows from the Titanic resource data:
tab = RandomSample[ResourceData["Sample Tabular Data: Titanic"], 8]Sort them by age, placing Missing[…] values at the end:
SortBy[tab, Function[#age]]SortBy[tab, "age"]Sort a list of vectors by their norm:
vectors = {{1 / 2, 1 / 3}, {1 / 4, 1 / 5}, {1 / 6, 1 / 7}};SortBy[vectors, Norm]Norm /@ %Their norms are sorted following canonical order, but not numerical order:
OrderedQ[%]N[%%]Explicitly request sorting of norms by numerical order:
SortBy[vectors, Norm, NumericalOrder]N[Norm /@ %]Sort complex numbers by their modulus using numerical order:
SortBy[{Sqrt[1 + Exp[-10I]], Exp[2 + I], Exp[2 I - 1], Exp[I]}, Abs, NumericalOrder]Abs[%]//NUsing canonical order gives a different result:
SortBy[{Sqrt[1 + Exp[-10I]], Exp[2 + I], Exp[2 I - 1], Exp[I]}, Abs]Abs[%]//NSortBy[{"dog", "zebra", "donkey", "anteater", "fish"}, StringLength]Sort according to decreasing lengths:
SortBy[{"dog", "zebra", "donkey", "anteater", "fish"}, StringLength, Greater]Generalizations & Extensions (1)
SortBy can be used on expressions with any head:
SortBy[g[h[1, 2], h[1, 1], h[2, 2], h[1]], Last]Applications (3)
Show the top 10 countries according to different criteria:
allcountries = CountryData[];topten[crit_, lors_ : "Largest"] := Module[{data}, data = SortBy[DeleteCases[Table[{c, CountryData[c, crit]}, {c, allcountries}], {_, _Missing}], Last];If[lors === "Largest", Take[data, {-1, -10, -1}], Take[data, 10]]]Sort the first 10 countries in alphabetical order by population:
TableForm[topten["Population"]]Sort the first 10 countries in alphabetical order by area:
TableForm[topten["Area", "Smallest"]]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}]]Sort by LeafCount:
SortBy[exprs, LeafCount]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.]];SortBy[exprs, err]Take a subset of the passengers of the Titanic:
dataset = Take[ExampleData[{"Dataset", "Titanic"}], 10]SortBy[dataset, {#sex&, #age&}]Properties & Relations (4)
Sort[list] is equivalent to SortBy[list,Identity]:
list = RandomInteger[10, 10]Sort[list]SortBy[list, Identity]SortBy[e,f] is equivalent to Sort[{f[#],#}&/@e][[All,-1]]:
e = RandomInteger[9, {10, 2}]SortBy[e, Last]Sort[{Last[#], #}& /@ e][[All, -1]]ReverseSortBy uses reverse canonical ordering:
list = {{a, y, 1}, {a, x, 1}, {b, x, 1}, {b, x, 2}};ReverseSortBy[list, First]SortBy[list, First]The result of ReverseSortBy[list,fs] is not always Reverse[SortBy[list,fs]]:
ReverseSortBy[list, {First, Last}]SortBy[list, {First, Last}]%% === Reverse[%]SortBy[list,f] is equivalent to list[[OrderingBy[list,f]]]:
list = Range[-5, 5]SortBy[list, Abs]list[[OrderingBy[list, Abs]]]Text
Wolfram Research (2007), SortBy, Wolfram Language function, https://reference.wolfram.com/language/ref/SortBy.html (updated 2019).
CMS
Wolfram Language. 2007. "SortBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/SortBy.html.
APA
Wolfram Language. (2007). SortBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SortBy.html
BibTeX
@misc{reference.wolfram_2026_sortby, author="Wolfram Research", title="{SortBy}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/SortBy.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_sortby, organization={Wolfram Research}, title={SortBy}, year={2019}, url={https://reference.wolfram.com/language/ref/SortBy.html}, note=[Accessed: 13-June-2026]}