thojanssens1

thojanssens1

Sort by multiple values?

Is there an easy way to sort a list of maps by some key, but if those keys are equal, be able to specify another key (and again if equal, another key, and so on)?

E.g.

fruits =
  [
    %{
      color: "yellow",
      name: "potato"
    },
    %{
      color: "red",
      name: "raspberry"
    },
    %{
      color: "red",
      name: "radish"
    },
    %{
      color: "yellow",
      name: "pineapple"
    },
  ]

First sort the maps by color ascending, but if equal, by name ascending.

Marked As Solved

hauleth

hauleth

I cannot find docs to back it up, but I believe that you want Enum.sort_by(list, &{&1.color, &1.name}).

17
Post #2

Also Liked

LostKobrakai

LostKobrakai

Tuples are ordered by size, two tuples with the same size are compared element by element.

sodapopcan

sodapopcan

You negate the terms:

Enum.sort_by(list, &{!&1.color, !&1.name})

lud

lud

No, this will sort that list: [{false, false},{false, false},{false, false},{false, false}].

You can just reverse the list. If you want to sort by ascending name but descending color you will have to compute a score or something like that, like mapping colors to integers and multiply them by -1 to get descending order.

Last Post!

LostKobrakai

LostKobrakai

At that point I think it makes sense to use a custom compare function, instead of trying to come up with some intermediary, which happens to be sorted correctly without one.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement