LexicographicSort[{e1,e2,…}]
sorts the list of expressions ei in lexicographic order.
LexicographicSort[{e1,e2,…},p]
compares elements of the ei expressions using the ordering function p.
LexicographicSort
LexicographicSort[{e1,e2,…}]
sorts the list of expressions ei in lexicographic order.
LexicographicSort[{e1,e2,…},p]
compares elements of the ei expressions using the ordering function p.
Details
- Lexicographic sorting is also known as lexical sorting and dictionary sorting.
- By default, LexicographicSort compares elements of the ei expressions using canonical Order.
- LexicographicSort[{string1,string2,…}] sorts by lexicographic order of the lists of characters of the strings.
- LexicographicSort[list,p] is equivalent to Sort[list,LexicographicOrder[p]].
Examples
open all close allBasic Examples (1)
Scope (5)
Sort lexicographically the elements of a list:
LexicographicSort[{{b, c}, {b, b, a, a}, {a, b, c}}]Use an ordering function to compare respective subexpressions of the elements:
LexicographicSort[{{Infinity, 0.}, {0, 0., 1.}, {-Infinity, 2., 2., 3.}}, NumericalOrder]Sort lexicographically expressions with heads other than List:
LexicographicSort[{h[b, c], h[b, b, a, a], h[a, b, c]}]Sort lexicographically a list of strings:
LexicographicSort[{"bob", "alice"}]Sort lexicographically a list of associations, comparing respective values:
LexicographicSort[{<|"c" -> 1|>, <|"a" -> 3, "b" -> 2|>}]Applications (1)
Properties & Relations (6)
Order is determined by the first element that differs, regardless of expression length:
LexicographicSort[{{b, a, c}, {b, a, b, c, d}, {b, a, a, c}}]When all elements coincide up to the shortest length, shorter lists are ordered first:
LexicographicSort[{{a, b, c, d}, {a}, {a, b, c}, {a, b}}]Empty lists are sorted before any other list:
LexicographicSort[{{a, b, c}, {}, {b, a}}]LexicographicSort[list] is equivalent to Sort[list,LexicographicOrder]:
SeedRandom[1234];
lists = RandomChoice[Alphabet[], #]& /@ RandomInteger[{2, 5}, 5]LexicographicSort[lists]Sort[lists, LexicographicOrder]Sort[lists]For lists of the same length, LexicographicSort is equivalent to Sort:
list1 = RandomChoice[Alphabet[], 5]list2 = RandomChoice[Alphabet[], 5]LexicographicSort[{list1, list2}]Sort[{list1, list2}]LexicographicSort with strings of letters is equivalent to AlphabeticSort with default options:
words = RandomWord[4]LexicographicSort[words]AlphabeticSort[words]AlphabeticSort and Sort are not lexicographic when the strings contain letters and numbers:
strings = {"A2", "a2", "A1", "a1"};LexicographicSort[strings]AlphabeticSort[strings]Sort[strings]Related Guides
History
Text
Wolfram Research (2021), LexicographicSort, Wolfram Language function, https://reference.wolfram.com/language/ref/LexicographicSort.html.
CMS
Wolfram Language. 2021. "LexicographicSort." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/LexicographicSort.html.
APA
Wolfram Language. (2021). LexicographicSort. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LexicographicSort.html
BibTeX
@misc{reference.wolfram_2026_lexicographicsort, author="Wolfram Research", title="{LexicographicSort}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/LexicographicSort.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_lexicographicsort, organization={Wolfram Research}, title={LexicographicSort}, year={2021}, url={https://reference.wolfram.com/language/ref/LexicographicSort.html}, note=[Accessed: 13-June-2026]}