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
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}]
6
Also Liked
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.
2
adamu
I said something similar 3 years ago
(actually @mudasobwa said it and I got the points. Thanks ![]()
).
1
dimitarvp
Sure, you can just put it in your project. Two minute job.
1
Popular in Questions
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
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
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
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
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New







