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

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
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
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
electic
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
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
fireproofsocks
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
romenigld
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

We're in Beta

About us Mission Statement