LexicographicOrder
✖
LexicographicOrder
gives Order[ai,bi] for the first non-coinciding pair ai,bi of elements, and 0 if the lists are identical.
represents an operator form that compares lists when applied to {a1,a2,…}, {b1,b2,…}.
Details

- Lexicographic order is also known as lexical order and dictionary order.
- Lexicographic order of two lists compares respective elements until one of the comparisons determines the order. If all elements coincide up to the length of the shorter list, that one is ordered first.
- By default, LexicographicOrder compares elements using canonical Order.
- LexicographicOrder[h[a1,a2,…],h[b1,b2,…],p] works for heads h other than List.
- LexicographicOrder[string1,string2] is equivalent to LexicographicOrder[Characters[string1],Characters[string2]].
- LexicographicOrder[p][list1,list2] is equivalent to LexicographicOrder[list1,list2,p].
Examples
open allclose allBasic Examples (1)Summary of the most common use cases
Scope (6)Survey of the scope of standard use cases
Use an ordering function to order elements of the expressions:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-4pgeqz

Canonical order places 0 before -Infinity:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-rorenr

Heads other than List can be used:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-s10te4

Use LexicographicOrder with two strings:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-4zwqta

The computation is equivalent to:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-j3y57i

Order associations lexicographically by their values:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-5jsjq1

Use LexicographicOrder in Ordering to find the position of the last expression in lexical order:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-12ud4

Check whether several lists are sorted lexicographically:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-p8dydf

Applications (2)Sample problems that can be solved with this function
Sort subsets lexicographically:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-9cnifv


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-09x1qp

Compare two monomials lexicographically:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-t600x8


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-7utndq

The first monomial is ordered first:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-2by8zx

Properties & Relations (9)Properties of the function, and connections to other functions
Order is determined by the first element that differs, regardless of total length:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-htexle

LexicographicOrder returns 0 when the lists have the same elements:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-fkd8qf

When all elements coincide up to the shortest length, the shorter list is ordered first:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-syl24g

The empty list is sorted before any other list:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-oxvuss


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-1pl2gk

LexicographicSort[list] is equivalent to Sort[list,LexicographicOrder]:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-gk3m9j


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-5ngtdw


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-haxngg


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-2xccfr

For lists of the same length, LexicographicOrder is equivalent to Order:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-2il30x


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-zeaquz


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-533grv


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-zuvo9h

LexicographicOrder with strings of letters is equivalent to AlphabeticOrder with default options:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-v4wd85


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-lfbimv


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-l5f5po

AlphabeticOrder and Order are not lexicographic when the strings contain letters and numbers:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-ky90hb


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-jibe02


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-sjh3ce

Compare with the ordering of the first characters:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-5y4btw

For numeric vectors of equal length, LexicographicOrder[NumericalOrder] is equivalent to NumericalOrder:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-gvasjk

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-kfsv2k


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-cjk3bk

VectorLess and related functions are similar to LexicographicOrder[NumericalOrder]:

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-httl5m

https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-dys4wd


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-zv54tz


https://wolfram.com/xid/0nx3iwmxgpx2rbgy6-dcs606

Wolfram Research (2021), LexicographicOrder, Wolfram Language function, https://reference.wolfram.com/language/ref/LexicographicOrder.html.
Text
Wolfram Research (2021), LexicographicOrder, Wolfram Language function, https://reference.wolfram.com/language/ref/LexicographicOrder.html.
Wolfram Research (2021), LexicographicOrder, Wolfram Language function, https://reference.wolfram.com/language/ref/LexicographicOrder.html.
CMS
Wolfram Language. 2021. "LexicographicOrder." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/LexicographicOrder.html.
Wolfram Language. 2021. "LexicographicOrder." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/LexicographicOrder.html.
APA
Wolfram Language. (2021). LexicographicOrder. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LexicographicOrder.html
Wolfram Language. (2021). LexicographicOrder. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LexicographicOrder.html
BibTeX
@misc{reference.wolfram_2025_lexicographicorder, author="Wolfram Research", title="{LexicographicOrder}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/LexicographicOrder.html}", note=[Accessed: 18-May-2025
]}
BibLaTeX
@online{reference.wolfram_2025_lexicographicorder, organization={Wolfram Research}, title={LexicographicOrder}, year={2021}, url={https://reference.wolfram.com/language/ref/LexicographicOrder.html}, note=[Accessed: 18-May-2025
]}