grangerelixx

grangerelixx

Enum.sort_by list of maps

I have a list of maps.

post_a =
%{
post_body_size: 4,
date: ~D[2021-06-05]
}
post_b =
%{
post_body_size: 4,
date: ~D[2021-06-04]
}
post_c =
%{
post_body_size: 4,
date: ~D[2021-06-03]
}
post_d =
%{
post_body_size: 5,
date: ~D[2021-06-02]
}

I am trying to organize the posts based on the body size (ascending order) and for cases when body sizes are same, the next sort_by is the date(descending order).

[post_count_1, post_count_2, post_count_3, post_count_4] =
        [post_a, post_b, post_c, post_d]
        |> Enum.sort_by(&{Map.fetch(&1, :post_body_size), {:desc, Map.fetch(&1, :date)}})

This doesn’t seem to sort the posts with (only) same body size according to date. Any inputs on how to resolve this or where I am possibly going wrong?

Most Liked

APB9785

APB9785

Creator of ECSx

In order to compare Date structs, you have to pass the Date module as the third argument to Enum.sort_by/3 as shown in the very last example from Enum - sort_by/3. But by doing this I’m not sure if you can do the Integer comparison for :post_body_size in the same function. Here’s a two-pass implementation:

post_list
|> Enum.sort_by(&Map.fetch!(&1, :date), {:desc, Date})
|> Enum.sort_by(&Map.fetch!(&1, :post_body_size))
grangerelixx

grangerelixx

I am sorry, its working, I have been trying to assert with the wrong posts. Your solution works for both the cases. Thanks a lot!

grangerelixx

grangerelixx

Got it, thank you so much. I got a real good understanding of this concept.

Where Next?

Popular in Questions 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
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
nsuchy
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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

We're in Beta

About us Mission Statement