SortBy

SortBy[list,f]
sorts the elements of list in the order defined by applying f to each of them.

SortBy[f]
represents an operator form of SortBy that can be applied to an expression.

DetailsDetails

  • SortBy[{e1,e2,e3,},f] sorts the so that the lie in canonical order.
  • If some of the are the same, then the canonical order of the corresponding is used.
  • SortBy[list,{f1,f2,}] breaks ties by successively using the values obtained from the .
  • 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].

ExamplesExamplesopen allclose all

Basic Examples  (3)Basic Examples  (3)

Sort a list of lists by the last element of each sublist:

In[1]:=
Click for copyable input
Out[1]=

Sort by the total of each sublist:

In[2]:=
Click for copyable input
Out[2]=

Sort a list of integers by their values modulo 3:

In[1]:=
Click for copyable input
Out[1]=

Sort elements of an Association by their values modulo 3:

In[1]:=
Click for copyable input
Out[1]=
Introduced in 2007
(6.0)
| Updated in 2014
(10.0)