Ordering
Details
- In a numerical list, Ordering[list,n] gives the positions of the n smallest elements. Ordering[list,-n] gives the positions of the n largest elements.
- If there are several smallest elements in list, Ordering[list,1] will give only the position of the one that appears first.
- list[[Ordering[list]]] is the same as Sort[list].
- Ordering[list,seq] is equivalent to Take[Ordering[list],seq].
- Ordering[list,UpTo[n]] returns n positions, or as many as are available.
- Ordering[list,All,p] gives the position at which all elements of list appear in Sort[list,p].
- Ordering can be used on expressions with any head, not only List.
Examples
open all close allBasic Examples (4)
Find the ordering that sorts a list:
Ordering[{c, a, b}]{c, a, b}[[{2, 3, 1}]]Find the positions of the 4 smallest elements in a list:
Ordering[{2, 6, 1, 9, 1, 2, 3}, 4]Find the position of the largest element:
Ordering[{2, 6, 1, 9, 1, 2, 3}, -1]Find the ordering of values in an Association:
Ordering[<|1 -> c, 2 -> a, 3 -> b|>]Scope (4)
Find positions of elements from the 4
smallest to the largest:
Ordering[{2, 6, 1, 9, 1, 2, 3}, {4, -1}]Find positions of elements in Sort[list,Greater]:
Ordering[{2, 6, 1, 9, 1, 2, 3}, All, Greater]Sort[{2, 6, 1, 9, 1, 2, 3}, Greater]Find the positions of the 6 smallest elements in a list, or as many as are available:
Ordering[{2, 6, 1, 9, 2}, UpTo[6]]Find the ordering of rows in a Tabular object:
Tabular[{{1, 2}, {2, 1}, {0, 4}}, {"a", "b"}]Ordering[%]Generalizations & Extensions (1)
Applications (3)
Find a permutation that sorts a list:
Ordering[{2, 6, 1, 9, 1, 2, 3}]{2, 6, 1, 9, 1, 2, 3}[[%]]Find the inverse of a permutation:
Ordering[{4, 5, 1, 2, 3}]{b, c, a, d, e}[[{4, 5, 1, 2, 3}]]%[[{3, 4, 5, 1, 2}]]Sort a list of lists with respect to a particular position:
sll[ll_, elem_] := ll[[Ordering[ll[[All, elem]]]]]sll[{{1, 2, 3}, {4, 5, 6}, {5, 4, 3}, {9, 5, 1}}, 2]The same as Sort, but Ordering keeps the original ordering when elements are the same:
Sort[{{1, 2, 3}, {4, 5, 6}, {5, 4, 3}, {9, 5, 1}}, (#1[[2]] < #2[[2]])&]Using Ordering this way is much faster for large sets of lists:
data = RandomInteger[9, {10 ^ 5, 4}];First /@ {Timing[sll[data, 3];], Timing[Sort[data, (#1[[2]] < #2[[2]])&]]}Properties & Relations (2)
Find the position of the maximum element:
Ordering[{4, 5, -2, 3}, -1]Position[{4, 5, -2, 3}, Max[{4, 5, -2, 3}]]list[[Ordering[list]]] is equivalent to Sort[list]:
list = RandomInteger[47, 13];Sort[list]list[[Ordering[list]]]See Also
Sort OrderingBy SortBy Max Min RankedMin RankedMax MaximalBy MinimalBy Position OrderedQ Median Order NumericalOrder AlphabeticOrder LexicographicOrder
Function Repository: Ranking
Tech Notes
History
Introduced in 2000 (4.1) | Updated in 2014 (10.0) ▪ 2015 (10.3)
Text
Wolfram Research (2000), Ordering, Wolfram Language function, https://reference.wolfram.com/language/ref/Ordering.html (updated 2015).
CMS
Wolfram Language. 2000. "Ordering." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/Ordering.html.
APA
Wolfram Language. (2000). Ordering. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Ordering.html
BibTeX
@misc{reference.wolfram_2026_ordering, author="Wolfram Research", title="{Ordering}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/Ordering.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_ordering, organization={Wolfram Research}, title={Ordering}, year={2015}, url={https://reference.wolfram.com/language/ref/Ordering.html}, note=[Accessed: 13-June-2026]}