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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews