KeySortBy
Examples
Basic Examples (5)
Sort the association entries by keys in reverse order:
KeySortBy[<|1 -> x, 2 -> y, 3 -> z|>, Minus]Sort the association by its keys modulo 3:
KeySortBy[<|2 -> a, 3 -> b, 1 -> c|>, Mod[#, 3]&]Sort the entries by the length of their keys:
KeySortBy[<|"aaa" -> 1, "ab" -> 2, "abcd" -> 3|>, StringLength]KeySortBy[{2 -> a, 3 -> b, 1 -> c}, -#&]KeySortBy[{2 -> a, 3 -> b, 1 -> c}, Mod[#, 3]&]Use the operator form of KeySortBy:
KeySortBy[Minus][<|1 -> x, 2 -> y, 3 -> z|>]Related Guides
History
Text
Wolfram Research (2014), KeySortBy, Wolfram Language function, https://reference.wolfram.com/language/ref/KeySortBy.html.
CMS
Wolfram Language. 2014. "KeySortBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/KeySortBy.html.
APA
Wolfram Language. (2014). KeySortBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/KeySortBy.html
BibTeX
@misc{reference.wolfram_2026_keysortby, author="Wolfram Research", title="{KeySortBy}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/KeySortBy.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_keysortby, organization={Wolfram Research}, title={KeySortBy}, year={2014}, url={https://reference.wolfram.com/language/ref/KeySortBy.html}, note=[Accessed: 13-June-2026]}