Skip to content

Sort ​

The elements of the collection in increasing order.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageSort!
Sort(collection)the elements in increasing numeric order.
Sort(collection, comparator)sorted by a custom Function comparator, e.g. descending order.
Sort(expr)the operands of any expression, sorted in place — not just a collection's.
Typed signatures
TypePackageApplies
((T, order: (((character) any -> unknown) | ((character, character) any -> boolean | number))?) -> T where T: string) & ((indexed_collection<T>, order: (((T) any -> unknown) | ((any, any) any -> boolean | number))?) -> list<T> where T: any)combinatoricsreplaces compute-enginealways
((T, order: (((character) any -> unknown) | ((character, character) any -> boolean | number))?) -> T where T: string) & ((indexed_collection<T>, order: (((T) any -> unknown) | ((any, any) any -> boolean | number))?) -> list<T> where T: any)compute-enginealways

Domain: Collections

Details
  • Idempotent: sorting an already-sorted collection changes nothing.
  • Sort(c)=At(c,Ordering(c))Sort(c) = At(c, Ordering(c)). See Ordering and At.
  • Sort(c, p) puts a before b when p(a, b) holds and p(b, a) does not. Elements p holds both ways (a non-strict p, like <=) keep their input order; elements it holds neither way (a strict p, like <) come out in reverse input order, as Wolfram's does.
  • Orders numbers numerically, strings in Wolfram's order (letters compare ignoring case, lowercase before uppercase on a tie, numbers before strings), and symbols alphabetically by name.
  • The comparator form Sort(list, p) takes a predicate p reporting whether a pair is already in order.
  • Works on the operands of any expression, not just a collection's elements.
  • Differs from Wolfram: strings sort by code point, so "B" comes before "a"; Wolfram's canonical order puts a, b, B.

Examples

1 more kept as data, checked against the oracles
Basic #
live ↗
InSort([3,1,2])\mathrm{Sort}(\bigl\lbrack3, 1, 2\bigr\rbrack)Out[1,2,3]\bigl\lbrack1, 2, 3\bigr\rbrack
live ↗
InSort([4,2,5,1,3])\mathrm{Sort}(\bigl\lbrack4, 2, 5, 1, 3\bigr\rbrack)Out[1,2,3,4,5]\bigl\lbrack1, 2, 3, 4, 5\bigr\rbrack

Symbols sort alphabetically

live ↗
InSort([d,b,c,a])\mathrm{Sort}(\bigl\lbrack d, b, c, a\bigr\rbrack)Out[a,b,c,d]\bigl\lbrack a, b, c, d\bigr\rbrack
live ↗
InSort([cat,fish,catfish,Cat])\mathrm{Sort}(\bigl\lbrack\text{cat}, \text{fish}, \text{catfish}, \text{Cat}\bigr\rbrack)Out[cat,Cat,catfish,fish]\bigl\lbrack\text{cat}, \text{Cat}, \text{catfish}, \text{fish}\bigr\rbrack
live ↗
InSort([b,1,a,2])\mathrm{Sort}(\bigl\lbrack\text{b}, 1, \text{a}, 2\bigr\rbrack)Out[1,2,a,b]\bigl\lbrack1, 2, \text{a}, \text{b}\bigr\rbrack
Scope #

A custom comparator sorts in a different order, here descending

live ↗
InSort([3,1,2],(_1⁡,_2⁡)↦_1⁡>_2⁡)\mathrm{Sort}(\bigl\lbrack3, 1, 2\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\operatorname{\_1}\gt\operatorname{\_2})Out[3,2,1]\bigl\lbrack3, 2, 1\bigr\rbrack

A list of strings sorts lexicographically

live ↗
InSort([banana,apple,cherry])\mathrm{Sort}(\bigl\lbrack\mathrm{banana}, \mathrm{apple}, \mathrm{cherry}\bigr\rbrack)Out[apple,banana,cherry]\bigl\lbrack\mathrm{apple}, \mathrm{banana}, \mathrm{cherry}\bigr\rbrack

Integers, rationals and floats together sort by value

live ↗
InSort([32,1,0.5,−2])\mathrm{Sort}(\bigl\lbrack\frac{3}{2}, 1, 0.5, -2\bigr\rbrack)Out[−2,0.5,1,32]\bigl\lbrack-2, 0.5, 1, \frac{3}{2}\bigr\rbrack

Exact numeric constants sort by their value

live ↗
InSort([π,e,2,3,1,2])\mathrm{Sort}(\bigl\lbrack π, e, 2, 3, 1, \sqrt{2}\bigr\rbrack)Out[1,2,2,e,3,π]\bigl\lbrack1, \sqrt{2}, 2, e, 3, π\bigr\rbrack

LessLess as the ordering function sorts by numeric value

live ↗
InSort([π,e,2,3,1,2],Less)\mathrm{Sort}(\bigl\lbrack π, e, 2, 3, 1, \sqrt{2}\bigr\rbrack, \mathrm{Less})Out[1,2,2,e,3,π]\bigl\lbrack1, \sqrt{2}, 2, e, 3, π\bigr\rbrack

A named ordering function: GreaterGreater sorts in descending order

live ↗
InSort([4,1,3,2,2],Greater)\mathrm{Sort}(\bigl\lbrack4, 1, 3, 2, 2\bigr\rbrack, \mathrm{Greater})Out[4,3,2,2,1]\bigl\lbrack4, 3, 2, 2, 1\bigr\rbrack

Sorting pairs by their second element with a custom comparator

live ↗
InSort([[a,2],[c,1],[d,3]],(_1⁡,_2⁡)↦_1⁡[2]<_2⁡[2])\mathrm{Sort}(\bigl\lbrack\bigl\lbrack a, 2\bigr\rbrack, \bigl\lbrack c, 1\bigr\rbrack, \bigl\lbrack d, 3\bigr\rbrack\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\operatorname{\_1}[2]\lt\operatorname{\_2}[2])Out[[c,1],[a,2],[d,3]]\bigl\lbrack\bigl\lbrack c, 1\bigr\rbrack, \bigl\lbrack a, 2\bigr\rbrack, \bigl\lbrack d, 3\bigr\rbrack\bigr\rbrack

Sorts the arguments of any head, not just a list

live ↗
InSort(f(b,a,c))\mathrm{Sort}(f(b, a, c))Outf(a,b,c)f(a, b, c)

A predicate that holds on a tie, like ≤\le, keeps input order

live ↗
InSort([[1,z],[2,b],[1,a],[1,"m"]],(_1⁡,_2⁡)↦_1⁡[1]≤_2⁡[1])\mathrm{Sort}(\bigl\lbrack\bigl\lbrack1, \text{z}\bigr\rbrack, \bigl\lbrack2, \text{b}\bigr\rbrack, \bigl\lbrack1, \text{a}\bigr\rbrack, \bigl\lbrack1, "m"\bigr\rbrack\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\operatorname{\_1}[1]≤\operatorname{\_2}[1])Out[[1,z],[1,a],[1,"m"],[2,b]]\bigl\lbrack\bigl\lbrack1, \text{z}\bigr\rbrack, \bigl\lbrack1, \text{a}\bigr\rbrack, \bigl\lbrack1, "m"\bigr\rbrack, \bigl\lbrack2, \text{b}\bigr\rbrack\bigr\rbrack

A predicate that fails both ways on a tie, like <<, puts ties in reverse input order

live ↗
InSort([[1,z],[2,b],[1,a],[1,"m"]],(_1⁡,_2⁡)↦_1⁡[1]<_2⁡[1])\mathrm{Sort}(\bigl\lbrack\bigl\lbrack1, \text{z}\bigr\rbrack, \bigl\lbrack2, \text{b}\bigr\rbrack, \bigl\lbrack1, \text{a}\bigr\rbrack, \bigl\lbrack1, "m"\bigr\rbrack\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\operatorname{\_1}[1]\lt\operatorname{\_2}[1])Out[[1,"m"],[1,a],[1,z],[2,b]]\bigl\lbrack\bigl\lbrack1, "m"\bigr\rbrack, \bigl\lbrack1, \text{a}\bigr\rbrack, \bigl\lbrack1, \text{z}\bigr\rbrack, \bigl\lbrack2, \text{b}\bigr\rbrack\bigr\rbrack
Applications #

Numbers sort by value, not magnitude

live ↗
InSort([−11,10,2,1,−4])\mathrm{Sort}(\bigl\lbrack-11, 10, 2, 1, -4\bigr\rbrack)Out[−11,−4,1,2,10]\bigl\lbrack-11, -4, 1, 2, 10\bigr\rbrack

Mixes exact logs and roots in true numeric order, e.g. ln⁡2<1<ln⁡3\ln 2 < 1 < \ln 3

live ↗
InSort(Join(ln⁡(1..10),1..10),Less)\mathrm{Sort}(\mathrm{Join}(\ln(1..10), \sqrt{1..10}), \mathrm{Less})Out[0,ln⁡(2),1,ln⁡(3),2ln⁡(2),2,ln⁡(5),3,ln⁡(6),ln⁡(7),2,3ln⁡(2),2ln⁡(3),5,ln⁡(10),6,7,22,3,10]\bigl\lbrack0, \ln(2), 1, \ln(3), 2\ln(2), \sqrt{2}, \ln(5), \sqrt{3}, \ln(6), \ln(7), 2, 3\ln(2), 2\ln(3), \sqrt{5}, \ln(10), \sqrt{6}, \sqrt{7}, 2\sqrt{2}, 3, \sqrt{10}\bigr\rbrack

Strings sort alphabetically, so a shorter prefix comes first

live ↗
InSort([aa,abb,ba,b,aaa])\mathrm{Sort}(\bigl\lbrack\text{aa}, \text{abb}, \text{ba}, \text{b}, \text{aaa}\bigr\rbrack)Out[aa,aaa,abb,b,ba]\bigl\lbrack\text{aa}, \text{aaa}, \text{abb}, \text{b}, \text{ba}\bigr\rbrack

A custom order compares absolute values

live ↗
InSort([−11,10,2,1,−4],(_1⁡,_2⁡)↦∣_1⁡∣<∣_2⁡∣)\mathrm{Sort}(\bigl\lbrack-11, 10, 2, 1, -4\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\vert\operatorname{\_1}\vert\lt\vert\operatorname{\_2}\vert)Out[1,2,−4,10,−11]\bigl\lbrack1, 2, -4, 10, -11\bigr\rbrack
live ↗
InSort(Join(ln⁡(1..10),1..10))\mathrm{Sort}(\mathrm{Join}(\ln(1..10), \sqrt{1..10}))Out[0,ln⁡(2),1,ln⁡(3),2ln⁡(2),2,ln⁡(5),3,ln⁡(6),ln⁡(7),2,3ln⁡(2),2ln⁡(3),5,ln⁡(10),6,7,22,3,10]\bigl\lbrack0, \ln(2), 1, \ln(3), 2\ln(2), \sqrt{2}, \ln(5), \sqrt{3}, \ln(6), \ln(7), 2, 3\ln(2), 2\ln(3), \sqrt{5}, \ln(10), \sqrt{6}, \sqrt{7}, 2\sqrt{2}, 3, \sqrt{10}\bigr\rbrack

Equal sort keys now follow Wolfram's tie order (our own convention changing, not a known value).

live ↗
InSort([aa,abb,ba,b,aaa],(_1⁡,_2⁡)↦StringLength(_1⁡)<StringLength(_2⁡))\mathrm{Sort}(\bigl\lbrack\text{aa}, \text{abb}, \text{ba}, \text{b}, \text{aaa}\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\mathrm{StringLength}(\operatorname{\_1})\lt\mathrm{StringLength}(\operatorname{\_2}))Out[b,ba,aa,aaa,abb]\bigl\lbrack\text{b}, \text{ba}, \text{aa}, \text{aaa}, \text{abb}\bigr\rbrack

Equal sort keys now follow Wolfram's tie order (our own convention changing, not a known value).

live ↗
InSort([i,1+i,1−i,2+3i],(_1⁡,_2⁡)↦ℜ(_1⁡)<ℜ(_2⁡))\mathrm{Sort}(\bigl\lbrack i, 1+i, 1-i, 2+3i\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\Re(\operatorname{\_1})\lt\Re(\operatorname{\_2}))Out[i,1−i,1+i,2+3i]\bigl\lbrack i, 1-i, 1+i, 2+3i\bigr\rbrack
Properties #

SortSort is idempotent: sorting an already-sorted collection changes nothing

live ↗
InSort(Sort([3,1,2]))=Sort([3,1,2])\mathrm{Sort}(\mathrm{Sort}(\bigl\lbrack3, 1, 2\bigr\rbrack))=\mathrm{Sort}(\bigl\lbrack3, 1, 2\bigr\rbrack)OutTrue\mathrm{True}

Sort(c)=At(c,Ordering(c))Sort(c) = At(c, Ordering(c)). See Ordering and At

live ↗
InSort([3,1,2])=[3,1,2][Ordering([3,1,2])]\mathrm{Sort}(\bigl\lbrack3, 1, 2\bigr\rbrack)=\bigl\lbrack3, 1, 2\bigr\rbrack[\mathrm{Ordering}(\bigl\lbrack3, 1, 2\bigr\rbrack)]OutTrue\mathrm{True}

The result doesn't depend on the input order

live ↗
InSort(Reverse([3,1,2]))=Sort([3,1,2])\mathrm{Sort}(\mathrm{Reverse}(\bigl\lbrack3, 1, 2\bigr\rbrack))=\mathrm{Sort}(\bigl\lbrack3, 1, 2\bigr\rbrack)OutTrue\mathrm{True}
live ↗
InSort(list)\mathrm{Sort}(\mathrm{list})OutSort(list)\mathrm{Sort}(\mathrm{list})
Possible issues #

The empty list sorts to itself

live ↗
InSort([])\mathrm{Sort}(\bigl\lbrack\bigr\rbrack)Out[]\bigl\lbrack\bigr\rbrack
live ↗
InSort([∞,2,1,2,−∞,22])\mathrm{Sort}(\bigl\lbrack∞, \sqrt{2}, 1, 2, -∞, \frac{\sqrt{2}}{2}\bigr\rbrack)Out[−∞,22,1,2,2,∞]\bigl\lbrack-∞, \frac{\sqrt{2}}{2}, 1, \sqrt{2}, 2, ∞\bigr\rbrack
About
Examples29
Checked againstWolfram Language 26 agree3 differmathlib4 6 agree3 error
CrosswalkWolfram Language

Implementation

mappedmathlib4packages/oracle/src/mappings.ts

A stable sort of exact rationals, increasing. Strings, symbols and nested lists fail to elaborate, since ours orders them by Wolfram's canonical order. A comparator is unbound.

See also: Ordering