OrderedQ
Details
- OrderedQ[{e,e}] gives True.
- By default, OrderedQ uses canonical order as described in the notes for Sort. This is equivalent to specifying Order as the ordering function p. »
- The ordering function p applied to a pair of elements e1, e2 may return either 1, 0,
or True, False. The value of p[e1,e2] is interpreted as follows: -
1 e1 comes before e2 0 e1 and e2 should be treated as identical -1 e1 comes after e2 True e1 and e2 are in order False e1 and e2 are out of order - If the ordering function p returns a value p[e1,e2] other than the preceding ones, then e1 and e2 are effectively treated as being in order. »
Examples
open all close allBasic Examples (4)
Check if a list of numbers is ordered:
OrderedQ[{1, 4, 2}]Check if a list of strings is ordered:
OrderedQ[{"cat", "catfish", "fish"}]Check if numerical expressions are sorted by their structure:
OrderedQ[{1, Sqrt[2], 2, E, 3, π}]Check if the numerical values are sorted:
OrderedQ[{1, Sqrt[2], 2, E, 3, π}, Less]Check if a list is ordered when only examining the second part of each element:
OrderedQ[{{a, 2}, {c, 1}, {d, 3}}, #1[[2]] < #2[[2]]&]Scope (8)
OrderedQ works with any expression:
OrderedQ[{x, y, x + y}]OrderedQ works with any head, not just List:
OrderedQ[f[b, a, c]]Check whether the values of an association are in order:
OrderedQ[<|1 -> 10, 2 -> 5, 3 -> 0|>]Specify Greater as the ordering function:
OrderedQ[{4, 3, 2, 1}, Greater]Use GreaterEqual to allow repeated elements:
OrderedQ[{4, 3, 3, 1}, Greater]OrderedQ[{4, 3, 3, 1}, GreaterEqual]Use NumericalOrder to allow complex numbers and number-like expressions:
OrderedQ[{1 + I, 2 - I, 3}, NumericalOrder]OrderedQ[{Yesterday, Today, Tomorrow}, NumericalOrder]Sort according to the rules of a particular language with AlphabeticOrder:
words = {"čeština", "hebrejčina", "hebrejčina", "chorvátčina"};OrderedQ[words, AlphabeticOrder["Slovak"]]OrderedQ[words, AlphabeticOrder["Serbian"]]Define a custom ordering function that puts symbols ahead of numbers:
p[_ ? NumberQ, _Symbol] := -1
p[_Symbol, _ ? NumberQ] := 1
p[_Symbol, _Symbol] := 0
p[x_ ? NumberQ, y_ ? NumberQ] := x ≤ yOrderedQ[{x, z, a, e, 1, 2, 5}, p]Use a pure function ordering function:
OrderedQ[{Entity["City", {"LosAngeles", "California", "UnitedStates"}], Entity["City", {"Chicago", "Illinois", "UnitedStates"}], Entity["City", {"Denver", "Colorado", "UnitedStates"}]}, Sign[#2["Elevation"] - #1["Elevation"]]&]Applications (2)
Properties & Relations (7)
OrderedQ[expr] is equivalent to OrderedQ[expr,Order]:
data = {1, I Sqrt[2], RGBColor[1, 0, 0], "word", {a, α, ℵ}};OrderedQ[data] == OrderedQ[data, Order]Comparisons are stopped as soon as it is determined that a pair is out of order:
OrderedQ[{1, 3, 5, 2, 7, 10}, (Print[{##}];Less[##])&]For explicit numbers, OrderedQ is effectively equivalent to LessEqual:
OrderedQ[{1, 2, 2, 4}]LessEqual[1, 2, 2, 4]For any association, OrderedQ[assoc,…]==OrderedQ[Values[assoc],…]:
assoc = <|c -> 1, b -> 2, a -> 3|>;OrderedQ[assoc] == OrderedQ[Values[assoc]]OrderedQ[expr] gives True when Sort[expr]===expr:
list = {I, 2, (1/Sqrt[3]), "hello", a, π, {1, 7}};OrderedQ[list]Sort[list] === listSort by default effectively sorts using OrderedQ:
Sort[{x, y, x ^ 2, y ^ 2, x + y}]Sort[{x, y, x ^ 2, y ^ 2, x + y}, OrderedQ[{#1, #2}]&]OrderedQ uses non-strict order by default:
OrderedQ[{a, b, b, c}]Check for strict canonical order by adding UnsameQ to the ordering function:
OrderedQ[{a, b, b, c}, OrderedQ[{#1, #2}] && UnsameQ[#1, #2]&]OrderedQ[{a, b, c}, OrderedQ[{#1, #2}] && UnsameQ[#1, #2]&]Alternatively, check whether Order gives 1:
OrderedQ[{a, b, b, c}, Order[#1, #2] == 1&]OrderedQ[{a, b, c}, Order[#1, #2] == 1&]Possible Issues (2)
OrderedQ by default works structurally, not by numerical value:
OrderedQ[{2, 4, E, Pi}]Numericize elements or use NumericalOrder to compare by numerical value:
OrderedQ[N[{2, 4, E, Pi}]]OrderedQ[{2, 4, E, Pi}, NumericalOrder]Unrecognized values of the ordering function are interpreted as elements being in order:
OrderedQ[{1, 2, a, 3, 4}, Echo[#1 < #2]&]Use TrueQ to interpret failed comparisons as being out of order:
OrderedQ[{1, 2, a, 3, 4}, TrueQ[#1 < #2]&]Tech Notes
Related Guides
History
Introduced in 1988 (1.0) | Updated in 2017 (11.1)
Text
Wolfram Research (1988), OrderedQ, Wolfram Language function, https://reference.wolfram.com/language/ref/OrderedQ.html (updated 2017).
CMS
Wolfram Language. 1988. "OrderedQ." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2017. https://reference.wolfram.com/language/ref/OrderedQ.html.
APA
Wolfram Language. (1988). OrderedQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/OrderedQ.html
BibTeX
@misc{reference.wolfram_2026_orderedq, author="Wolfram Research", title="{OrderedQ}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/OrderedQ.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_orderedq, organization={Wolfram Research}, title={OrderedQ}, year={2017}, url={https://reference.wolfram.com/language/ref/OrderedQ.html}, note=[Accessed: 13-June-2026]}