Mathematica > Core Language > List Manipulation > Rearranging & Restructuring Lists > SortBy >
Mathematica > Data Manipulation > Handling Arrays of Data > Rearranging & Restructuring Lists > SortBy >
Mathematica > Data Manipulation > Image Processing & Analysis > Basic Image Manipulation > Handling Arrays of Data > Rearranging & Restructuring Lists > SortBy >

SortBy

SortBy
sorts the elements of list in the order defined by applying f to each of them.
  • SortBy 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 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.
Sort a list of lists by the last element of each sublist:
Sort by the total of each sublist:
Sort a list of integers by their values modulo 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 by numerical value:
The default order ranks these symbolically:
Sort by absolute numerical value:
Slightly faster way:
Sort by Re, Im, Arg, and Abs:
Sort by date:
SortBy can be used on expressions with any head:
Show the top 10 countries according to different criteria:
Sort the first 10 countries in alphabetical order by population:
Sort the first 10 countries in alphabetical order by area:
Sort mathematically equivalent expressions by different criteria:
Sort by LeafCount:
Sort by size of error for numerical evaluation with machine numbers at :
SortBy is equivalent to Sort[{f[#], #}&/@e][[All, -1]]:
New in 6 | Last modified in 7
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF