OrderingBy

OrderingBy[list,f]

gives the positions in list at which each successive element of SortBy[list,f] appears.

OrderingBy[list,f,n]

gives the positions in list at which the first n elements of SortBy[list,f] appear.

OrderingBy[list,f,-n]

gives the positions of the last n elements of SortBy[list,f].

OrderingBy[list,f,n,p]

gives positions in list of elements of SortBy[list,f,p].

Details

  • list[[OrderingBy[list,f]]] is the same as SortBy[list,f].
  • OrderingBy[list,f,seq] is equivalent to Take[OrderingBy[list,f],seq].
  • OrderingBy[list,f,All,p] gives the position at which all elements of list appear in SortBy[list,f,p].
  • OrderingBy can be used on expressions with any head, not only List.

Examples

open all close all

Basic Examples  (3)

Find the ordering that sorts a list of strings by their length:

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

Apply the ordering:

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

Check that their string lengths are sorted:

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

Find the positions of the 3 smallest elements by absolute value:

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

Find the positions of the 3 largest elements by absolute value:

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

Find the ordering of values in an association:

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

Scope  (2)

Introduced in 2019
(12.0)