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

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chewm
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
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

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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
AstonJ
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

We're in Beta

About us Mission Statement