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?