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.

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

17 Likes

Thanks @hauleth for all the help you provide :+1: :+1: :+1:

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

https://erlang.org/doc/reference_manual/expressions.html#term-comparisons

5 Likes

How would you control the descending / ascending order?

You negate the terms:

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

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.

1 Like

I tried it out before replying—copy/pasted from my terminal:

iex(10)> [%{color: "yellow"}, %{color: "red"}] |> Enum.sort_by(&{!&1.color})
[%{color: "yellow"}, %{color: "red"}]
iex(12)> [%{color: "yellow", name: "potato"}, %{color: "red", name: "apple"}] |> Enum.sort_by(&{!&1.color, !&1.name})
[%{color: "yellow", name: "potato"}, %{color: "red", name: "apple"}]

sort_by is stable if EVERYTHING returns the same value:

iex(1)> [%{color: "yellow", name: "potato"}, %{color: "red", name: "apple"}] |> Enum.reverse() |> Enum.sort_by(&{!&1.color, !&1.name})

[%{color: "red", name: "apple"}, %{color: "yellow", name: "potato"}]

This should have returned the map with “yellow” first (like in your second example) if it was meaningfully sorting.

1 Like

Yes,

Both expressions return the original list:

[%{color: "yellow"}, %{color: "red"}] |> Enum.sort_by(&{!&1.color})
[%{color: "red"}, %{color: "yellow"}] |> Enum.sort_by(&{!&1.color})

Ah right—my bad :grimacing:

The negation trick works for floats and integers, using minus operator. Not sure how to control desc/asc for other data types. :man_shrugging:

Enum.sort/2 comes with built in sort order handling nowadays:

https://hexdocs.pm/elixir/Enum.html#sort/2-ascending-and-descending-since-v1-10-0

I meant for sort_by when you need to sort by multiple values but some should be by asc, other’s by desc. The control of that is not built in, the ! negation does not work but - does, at least for numbers.

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.

1 Like