# Ordered sorting of a map to a keyword list

Just wondered if there is some standard library function for converting a map to a keyword list, but sorting it on the way.

something like;

``````map = %{"z" => 4, "y" => 6, "x" => 7}
order = ["x", "y"]
output = new_func(map, order)
# output now equals [{"x", 7}, {"y", 6}]

``````

Here’s one approach:

``````iex(1)> map = %{"z" => 4, "y" => 6, "x" => 7}
%{"x" => 7, "y" => 6, "z" => 4}
iex(2)> order = ["x", "y"]
["x", "y"]
iex(3)> Enum.map(order, fn key -> {key, Map.get(map, key)} end)
[{"x", 7}, {"y", 6}]
``````
5 Likes

No the stdlib does not have that and you’ll see exactly why: it’s easy to do it yourself.

``````%{"z" => 4, "y" => 6, "x" => 7}
|> Map.take(["x", "y"])   # only take keys "x" and "y"
|> Map.to_list()          # convert the map to a list of tuples
|> Enum.sort_by(fn {key, _value} -> key end) # sort the list of tuples by their first element
``````

And you will get `[{"x", 7}, {"y", 6}]` as your result.

2 Likes

You don’t need to convert the map to a list because maps are enumerable.

I said something similar 3 years ago (actually @mudasobwa said it and I got the points. Thanks  ).

1 Like

I keep forgetting. I just like convenience functions, like how a map has keys() and values() when you could implement them yourself

Sure, you can just put it in your project. Two minute job.

1 Like

That’s super inefficient though. If you had e.g. a map with 20 elements and wanted 10 of them, that solution in SO would do 10 map lookups.

Saving a line or two of code just to make the complexity of the code something almost O(n^2) is a bad practice. Whereas the `Map.take` + `Enum.sort_by` is not even two full passes of the underlying list.

Let’s not discuss further if nobody’s willing to actually run the numbers 