Question: I have an association that maps `{x, y}` pairs to values, for example:

```
 <|{0, 0} -> "a", {-1, -4} -> "b", {-1, 1} -> "c", {-1, 0} -> "d", {1, 0} -> "e"|>

```

I want to convert this to a nested association with the outer association keyed by `x` and the inner by `y`:

```
<|0 -> <|0 -> "a"|>, -1 -> <|-4 -> "b", 0 -> "d", 1 -> "c"|>, 1 -> <|0 -> "e"|>|>

```

My current code is (assuming `a` is the association):

```
Apply[Association]@*Map[Last@# -> a[#] &] /@ GroupBy[Keys@a, First, Sort]

```

but I'm wondering if there's a more elegant way to do this (sorting not necessary).

Answer: 
The following will do the same thing and avoid the sorting, but I am not sure whether it counts as "more elegant" in your book:

```
assoc = <|{0, 0} -> "a", {-1, -4} -> "b", {-1, 1} -> "c", {-1, 0} -> "d", {1, 0} -> "e"|>;

Association /@
 GroupBy[#[[1, 1]]& -> (#[[1, 2]] -> #[[2]] &)]@
  Normal@assoc

(*Out:  <|0 -> <|0 -> "a"|>, -1 -> <|-4 -> "b", 1 -> "c", 0 -> "d"|>, 1 -> <|0 -> "e"|>|> *)

```
```
keyTrie=Query[keyGroupBy[First]]/*Query[All,KeyMap[Rest]]/*Query[All,If[Keys[#1]=={{}},First[Values[#1]],keyTrie[#1]]&]

```

This is a recursive query that uses `keyGroupBy` utility:

```
keyGroupBy[f_][expr_]:=Association/@GroupBy[Normal[expr],Keys/*f]

```

To your data:

```
<|{0, 0} -> "a", {-1, -4} -> "b", {-1, 1} -> "c", {-1, 0} -> 
   "d", {1, 0} -> "e"|> // keyTrie

```

> 
> <|0 -> <|0 -> "a"|>, -1 -> <|-4 -> "b", 1 -> "c", 0 -> "d"|>, 1 ->
>  <|0 -> "e"|>|>
> 
> 
>