({{, (RefLink[KeySort, paclet:ref/KeySort])[assoc] 
orders the elements of an association by sorting its keys.}, {, (RefLink[KeySort, paclet:ref/KeySort])[assoc,p]
orders the elements of an association using the ordering function p.}})

OpenersNotesSection(Spacer1)Details

(RefLink[KeySort, paclet:ref/KeySort])[assoc] returns an (RefLink[Association, paclet:ref/Association]) object formed in effect by applying (RefLink[Sort, paclet:ref/Sort]) to (RefLink[Keys, paclet:ref/Keys])[assoc].

In (RefLink[KeySort, paclet:ref/KeySort])[assoc,p], the ordering function p is applied to pairs of keys using the same specifications as in (RefLink[Sort, paclet:ref/Sort]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (4)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Sort the elements in an association by their keys:

KeySort[<|b->y,c->z,a->x|>]

Sort a list of rules:

KeySort[{b->y,c->z,a->x}]

OpenersExampleSection1(Spacer1)Scope  (1)

Construct an association with Spanish words as keys:

assoc=<|"año"->1,"anulado"->2,"amigo"->3|>

Sorting the keys with Spanish alphabetic order is different from sorting with the default English ordering:

KeySort[assoc,AlphabeticOrder["Spanish"]]

KeySort[assoc]

OpenersExampleSection3(Spacer1)Properties & Relations  (1)

Use (RefLink[KeySort, paclet:ref/KeySort]) to sort an association by keys:

assoc=<|"b"->3,"a"->2,"c"->1|>

KeySort[assoc]

Use (RefLink[Sort, paclet:ref/Sort]) to sort the association by values:

Sort[assoc]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[KeySortBy, paclet:ref/KeySortBy])  ▪  (RefLink[Sort, paclet:ref/Sort])  ▪  (RefLink[Keys, paclet:ref/Keys])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Associations, paclet:guide/Associations])}, {▪(RefLinkPlain[Text Analysis, paclet:guide/TextAnalysis])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/34-associations.html])(WebLink[: Associations, https://www.wolfram.com/language/elementary-introduction/34-associations.html])}})}})