Anko

Anko

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}]

Marked As Solved

codeanpeace

codeanpeace

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}]

Also Liked

dimitarvp

dimitarvp

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.

adamu

adamu

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

dimitarvp

dimitarvp

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

Last Post!

adamu

adamu

I didn’t benchmark it, but it’s nlogn, which is about par for the course for sorting in immutable languages AFAIK. Premature optimisation, etc…

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

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement