Skip to content

Ordering ​

The permutation of indices that would sort the collection into increasing order.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageOrdering✓
Ordering(collection)the permutation of indices that sorts the collection into increasing order.
Ordering(collection, n)just the first n indices of the full ordering; a negative n takes the last |n| (the largest elements); {m, n} takes positions m through n of the full ordering; UpTo(n) caps at n without erroring on a shorter collection.
Ordering(collection, All, Greater)every index, ordered descending rather than ascending.
Typed signatures
TypePackageApplies
(indexed_collection<T>, order: (((T) any -> unknown) | ((any, any) any -> boolean | number))?) -> list<integer> where T: anycompute-enginealways
(indexed_collection<any>, any?, any?) -> list<integer>combinatoricsreplaces compute-enginealways

Domain: Collections

Details
  • c[[Ordering(c)]]=Sort(c)c[[Ordering(c)]] = Sort(c): applying the permutation at those positions recovers Sort's result.
  • Ties break in favor of earlier position, i.e. it's a stable ordering.
  • Symbols order alphabetically by name, matching Wolfram's canonical ordering.
  • A second argument nn takes just the first nn indices of the full ordering; see the signatures above for the negative-count, {m,n}\{m, n\}, and UpToUpTo forms.

Examples

Basic #
live ↗
InOrdering([3,1,2])\mathrm{Ordering}(\bigl\lbrack3, 1, 2\bigr\rbrack)Out[2,3,1]\bigl\lbrack2, 3, 1\bigr\rbrack
live ↗
InOrdering([2,6,1,9,1,2,3])\mathrm{Ordering}(\bigl\lbrack2, 6, 1, 9, 1, 2, 3\bigr\rbrack)Out[3,5,1,6,7,2,4]\bigl\lbrack3, 5, 1, 6, 7, 2, 4\bigr\rbrack
Scope #

Ties break in favor of the earlier position: both 1s come before both 2s

live ↗
InOrdering([2,1,2,1])\mathrm{Ordering}(\bigl\lbrack2, 1, 2, 1\bigr\rbrack)Out[2,4,1,3]\bigl\lbrack2, 4, 1, 3\bigr\rbrack

A second argument takes just the first n indices of the full ordering

live ↗
InOrdering([2,6,1,9,1,2,3],4)\mathrm{Ordering}(\bigl\lbrack2, 6, 1, 9, 1, 2, 3\bigr\rbrack, 4)Out[3,5,1,6]\bigl\lbrack3, 5, 1, 6\bigr\rbrack

Symbols order alphabetically

live ↗
InOrdering([c,a,b])\mathrm{Ordering}(\bigl\lbrack c, a, b\bigr\rbrack)Out[2,3,1]\bigl\lbrack2, 3, 1\bigr\rbrack

A negative count gives the positions of the largest elements

live ↗
InOrdering([2,6,1,9,1,2,3],−1)\mathrm{Ordering}(\bigl\lbrack2, 6, 1, 9, 1, 2, 3\bigr\rbrack, -1)Out[4]\bigl\lbrack4\bigr\rbrack

A {m,n}\{m, n\} spec takes the 4th through the last entries of the ordering

live ↗
InOrdering([2,6,1,9,1,2,3],[4,−1])\mathrm{Ordering}(\bigl\lbrack2, 6, 1, 9, 1, 2, 3\bigr\rbrack, \bigl\lbrack4, -1\bigr\rbrack)Out[6,7,2,4]\bigl\lbrack6, 7, 2, 4\bigr\rbrack

A third argument orders by a custom test, here descending

live ↗
InOrdering([2,6,1,9,3],All,Greater)\mathrm{Ordering}(\bigl\lbrack2, 6, 1, 9, 3\bigr\rbrack, \mathrm{All}, \mathrm{Greater})Out[4,2,5,1,3]\bigl\lbrack4, 2, 5, 1, 3\bigr\rbrack

UpTo(6) asks for at most 6 positions, so a 5-element list gives all 5

live ↗
InOrdering([2,6,1,9,2],UpTo(6))\mathrm{Ordering}(\bigl\lbrack2, 6, 1, 9, 2\bigr\rbrack, \mathrm{UpTo}(6))Out[3,1,5,2,4]\bigl\lbrack3, 1, 5, 2, 4\bigr\rbrack
Applications #

The position of the smallest element. See First

live ↗
InFirst(Ordering([2,6,1,9,1,2,3]))\mathrm{First}(\mathrm{Ordering}(\bigl\lbrack2, 6, 1, 9, 1, 2, 3\bigr\rbrack))Out33

Positions in increasing order of value: the 11 is third, then 22, 33, 44, 55

live ↗
InOrdering([4,5,1,2,3])\mathrm{Ordering}(\bigl\lbrack4, 5, 1, 2, 3\bigr\rbrack)Out[3,4,5,1,2]\bigl\lbrack3, 4, 5, 1, 2\bigr\rbrack
Properties #

On a permutation, OrderingOrdering is the inverse, so applying it twice recovers the permutation

live ↗
InOrdering(Ordering([3,1,2]))\mathrm{Ordering}(\mathrm{Ordering}(\bigl\lbrack3, 1, 2\bigr\rbrack))Out[3,1,2]\bigl\lbrack3, 1, 2\bigr\rbrack

A negative count takes from the largest end, so the position of 55

live ↗
InOrdering([4,5,−2,3],−1)\mathrm{Ordering}(\bigl\lbrack4, 5, -2, 3\bigr\rbrack, -1)Out[2]\bigl\lbrack2\bigr\rbrack
About
Examples13
Checked againstWolfram Language 13 agree
CrosswalkWolfram Language

See also: Sort, UpTo