jpcaruana

jpcaruana

Sort a list of maps like [%{"some string" => weight}]

I would like to sort that kind of List of weighted maps:

[
  %{"this is a string" => 24},
  %{"en français" => 11}
]

into a weight ordered List of keys: ["en français", "this is a string"]

I don’t the key names in advance.

Marked As Solved

gregvaughn

gregvaughn

iex(72)> input = [
...(72)>   %{"this is a string" => 24},
...(72)>   %{"en français" => 11}
...(72)> ]
[%{"this is a string" => 24}, %{"en français" => 11}]
iex(73)> input |> Enum.flat_map(&Enum.to_list/1) |> Enum.sort_by(&elem(&1, 1)) |> Enum.map(&elem(&1, 0))
["en français", "this is a string"]

Also Liked

fceruti

fceruti

A little advice I can give you to analyze @gregvaughn 's code, is to use IO.inspect().

If you run the code as is, it’s not very informative, but if you inspect in between steps, you can get a much better understanding of what’s going on.

iex> input
|> IO.inspect(label: "input")
|> Enum.flat_map(&Enum.to_list/1)
|> IO.inspect(label: "Enum.flat_map")
|> Enum.sort_by(&elem(&1, 1))
|> IO.inspect(label: "Enum.sort_by")
|> Enum.map(&elem(&1, 0))

input: [%{"this is a string" => 24}, %{"en français" => 11}]
Enum.flat_map: [{"this is a string", 24}, {"en français", 11}]
Enum.sort_by: [{"en français", 11}, {"this is a string", 24}]
["en français", "this is a string"]
gregvaughn

gregvaughn

Yes, IO.inspect is great advice.

In this specific case, I converted the given input data into a more uniform/useful data structure, a list of 2 element tuples {string, weight}. Then I sorted by the 2nd element, then I kept the 1st element.

jpcaruana

jpcaruana

Thanks you for the fast answer! It works very fine, now I need to dig into it a little bit to understand it fully… so I won’t ask for help next time.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
gshaw
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
bsollish-terakeet
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
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lk-geimfari
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
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement