grangerelixx

grangerelixx

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?

Showing Posts 1 to 10

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 OP

Thank you so much This worked for when I want to sort the post by post_body_size ascending and date desc. I also have another condition where I want to order post_body_size by descending where the integer comparison doesn’t work

APB9785

APB9785

Creator of ECSx

You can sort :post_body_size like

...
|> Enum.sort_by(&Map.fetch!(&1, :post_body_size), :desc)

and it will be stable because :desc is a convenience for >=/2

grangerelixx

grangerelixx OP

I tried this but it doesn’t order correctly

APB9785

APB9785

Creator of ECSx

What exactly are the sort conditions?

grangerelixx

grangerelixx OP

I have four posts and I want to able to order it according to both asc and desc post_body_size. The default date order is desc so if the post_body_sizes are same, it defaults to order according to date desc(only the posts that have same post_body_size). I need the top two posts in both the cases.

This is for test.

APB9785

APB9785

Creator of ECSx

I’m still having a little trouble understanding. Can you show the test code you’ve already written?

grangerelixx

grangerelixx OP

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 OP

Can this be done in a single line? Like

|> Enum.sort_by(& {&1.date, {:desc, Date}, &1.post_body_size, :desc})

This is a wrong syntax but is there a right way to do it?

APB9785

APB9785

Creator of ECSx

Looking at the source for Enum.sort_by/3, I didn’t see any way to do it, because the function expects three arguments:

  • the Enumerable to be sorted
  • the mapper, which retrieves the desired elements
  • the sorter, which compares the elements.

You are able to “overload” the mapper with a tuple of elements, because the sorter will apply to each element in the tuple one-by-one. But the same sorter applies to all elements in the tuple. So in your case, where you want to use two different sorters (>=/2 for Integers, and Date.compare/2 for Dates), I think you must call the function twice.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews