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?
Trending in Questions
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
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
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
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
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
APB9785
In order to compare Date structs, you have to pass the
Datemodule as the third argument toEnum.sort_by/3as 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_sizein the same function. Here’s a two-pass implementation:grangerelixx
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
You can sort
:post_body_sizelikeand it will be stable because
:descis a convenience for>=/2grangerelixx
I tried this but it doesn’t order correctly
APB9785
What exactly are the sort conditions?
grangerelixx
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
I’m still having a little trouble understanding. Can you show the test code you’ve already written?
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
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
Looking at the source for
Enum.sort_by/3, I didn’t see any way to do it, because the function expects three arguments:Enumerableto be sortedmapper, which retrieves the desired elementssorter, which compares the elements.You are able to “overload” the
mapperwith a tuple of elements, because thesorterwill apply to each element in the tuple one-by-one. But the samesorterapplies to all elements in the tuple. So in your case, where you want to use two different sorters (>=/2for Integers, andDate.compare/2for Dates), I think you must call the function twice.