Function Repository Resource:

KeySortLike

Source Notebook

Sort keys of an Association in the same order as another set of keys

Contributed by: Bob Sandheinrich

ResourceFunction["KeySortLike"][assoc,order]

sorts elements of assoc so their keys match order.

ResourceFunction["KeySortLike"][order]

gives an operator that sorts associations like order.

Details and Options

The assoc can be an Association or List of rules.
The order can be a List of keys or an Association. In the case of an Association, the values of order are irrelevant.
ResourceFunction["KeySortLike"][order][assoc] is equivalent to ResourceFunction["KeySortLike"][assoc,order].
Elements not included in order are included at the end of the result with their order preserved.

Examples

Basic Examples (2) 

Sort an Association like a List:

In[1]:=
ResourceFunction[
 "KeySortLike"][<|"a" -> 10, "b" -> 20, "c" -> 30|>, {"b", "c", "a"}]
Out[1]=

Create an operator:

In[2]:=
sorter = ResourceFunction[
  "KeySortLike"][{"Apples", "Bananas", "Grapes"}]
Out[2]=

Sort some fruit:

In[3]:=
sorter[<|"Watermelon" -> Entity[
   "Food", {EntityProperty[
      "Food", "FoodType"] -> ContainsExactly[{
Entity[
        "FoodType", "Watermelon"]}], EntityProperty[
      "Food", "AddedFoodTypes"] -> ContainsExactly[{}]}], "Grapes" -> Entity[
   "Food", {EntityProperty[
      "Food", "FoodType"] -> ContainsExactly[{
Entity[
        "FoodType", "Grape"]}], EntityProperty[
      "Food", "AddedFoodTypes"] -> ContainsExactly[{}]}], "Apples" -> Entity[
   "Food", {EntityProperty[
      "Food", "FoodType"] -> ContainsExactly[{
Entity[
        "FoodType", "Apple"]}], EntityProperty[
      "Food", "AddedFoodTypes"] -> ContainsExactly[{}]}], "Blueberries" -> Entity[
   "Food", {EntityProperty[
      "Food", "FoodType"] -> ContainsExactly[{
Entity[
        "FoodType", "Blueberry"]}], EntityProperty[
      "Food", "AddedFoodTypes"] -> ContainsExactly[{}]}]|>]
Out[3]=

Scope (7) 

Associations can be used to specify the order as well. Create two associations:

In[4]:=
hrleaders = <|"Bonds" -> 762, "Aaron" -> 755, "Ruth" -> 714, "Rodriguez" -> 696, "Mays" -> 660, "Pujols" -> 656|>;
rbileaders = <|"Aaron" -> 2297, "Ruth" -> 2214, "Rodriguez" -> 2086, "Pujols" -> 2075, "Bonds" -> 1996|>;

Sort the all-time Major League Baseball home run records in the order of the all time runs batted in records:

In[5]:=
ResourceFunction["KeySortLike"][hrleaders, rbileaders]
Out[5]=

Sort integers based on an existing List:

In[6]:=
l = RandomInteger[10, 5]
Out[6]=
In[7]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/0b6e7108-aa45-4ab3-b0da-dbb5972b77ea"]
Out[7]=

Sort names according to an existing List containing only some of the keys:

In[8]:=
ResourceFunction[
 "KeySortLike"][<|"Bob" -> 1, "Rick" -> 2, "Billy" -> 3, "Danny" -> 4,
   "Paco" -> 5, "Katja" -> 6|>, {"Billy", "Rick", "Danny"}]
Out[8]=

Sort names according to an existing List containing extra keys:

In[9]:=
ResourceFunction[
 "KeySortLike"][<|"Billy" -> 1, "Katja" -> 2|>, {"Billy", "Rick", "Danny", "Bob", "Rick"}]
Out[9]=

Create an operator from an Association where the values are ignored:

In[10]:=
sorter = ResourceFunction[
  "KeySortLike"][<|"first" -> 1, "second" -> 2, "last" -> Infinity|>]
Out[10]=

Apply it to data:

In[11]:=
sorter[<|"second" -> 200, "third" -> 300, "first" -> 100|>]
Out[11]=

Sort rows of a Dataset using the operator form:

In[12]:=
Dataset[{
   <|"a" -> 1, "b" -> "x", "c" -> {1}|>,
   <|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>}][All, ResourceFunction["KeySortLike"][{"b", "c", "a"}]]
Out[12]=

KeySortLike works on a Dataset which contains indexed tables (associations of associations). Get an indexed table dataset:

In[13]:=
planets = ResourceData["Sample Data: Solar System Planets and Moons"]
Out[13]=

Sort the planets in the order that their names appear on the Wikipedia page for Roman deities:

In[14]:=
ResourceFunction[
 "KeySortLike"][planets, {"Jupiter", "Neptune", "Mars", "Venus", "Mercury"}]
Out[14]=

Properties and Relations (1) 

KeySortLike effectively applies the resource function SortLike to the keys of an Association:

In[15]:=
Keys@ResourceFunction[
  "KeySortLike"][<|"a" -> 10, "b" -> 20, "c" -> 30, "d" -> 40|>, {"b",
    "d", "a"}]
Out[15]=
In[16]:=
ResourceFunction["SortLike"][
 Keys@<|"a" -> 10, "b" -> 20, "c" -> 30, "d" -> 40|>, {"b", "d", "a"}]
Out[16]=

Possible Issues (2) 

KeySortLike fails for non-indexed datasets:

In[17]:=
ds = Dataset[{
    <|"a" -> 1, "b" -> "x", "c" -> {1}|>,
    <|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>}];
In[18]:=
ResourceFunction["KeySortLike"][ds, {"b", "c", "a"}]
Out[18]=

It can work as an operator at lower levels:

In[19]:=
ds[All, ResourceFunction["KeySortLike"][{"b", "c", "a"}]]
Out[19]=

Version History

  • 1.0.0 – 12 May 2020

Related Resources

License Information