Function Repository Resource:

# KeySortLike

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]:=
 Out[1]=

Create an operator:

 In[2]:=
 Out[2]=

Sort some fruit:

 In[3]:=
 Out[3]=

### Scope (7)

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

 In[4]:=

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

 In[5]:=
 Out[5]=

Sort integers based on an existing List:

 In[6]:=
 Out[6]=
 In[7]:=
 Out[7]=

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

 In[8]:=
 Out[8]=

Sort names according to an existing List containing extra keys:

 In[9]:=
 Out[9]=

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

 In[10]:=
 Out[10]=

Apply it to data:

 In[11]:=
 Out[11]=

Sort rows of a Dataset using the operator form:

 In[12]:=
 Out[12]=

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

 In[13]:=
 Out[13]=

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

 In[14]:=
 Out[14]=

### Properties and Relations (1)

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

 In[15]:=
 Out[15]=
 In[16]:=
 Out[16]=

### Possible Issues (2)

KeySortLike fails for non-indexed datasets:

 In[17]:=
 In[18]:=
 Out[18]=

It can work as an operator at lower levels:

 In[19]:=
 Out[19]=

## Version History

• 1.0.0 – 12 May 2020