Add Enum.reverse_sort/1 and Enum.reverse_sort_by/2

46 views
Skip to first unread message

José Valim

unread,
Oct 17, 2019, 4:39:42 PM10/17/19
to elixir-l...@googlegroups.com
I would like to propose a new small function to Enum, called Enum.reverse_sort, that returns sort in the reverse (descending order).

While reversing sorting today is easy to achieve with:

Enum.sort(collection, &>=/2)

I always have a second guess on what the second argument really is. I believe reverse_sort is much clearer in intent:

Enum.reverse_sort(collection)

This is clearer with sort_by too. instead of:

Enum.sort_by(users, & &1.name, &>=/2)

I can write:

Enum.reverse_sort_by(users, & &1.name)

The implementation itself is straight-forward.

What I would like to know is:

1. Is this a good addition?
2. Is the name reasonable? Any other takers?

José Valim
Skype: jv.ptec
Founder and Director of R&D
Message has been deleted

OvermindDL1

unread,
Oct 17, 2019, 6:44:52 PM10/17/19
to elixir-lang-core
I've wanted such a function a few times because passing in the alternated function is not always clear.  Instead of `&>=/2` I'd rather do things like `&not(&1<&2)` as it makes it unambiguous while reading, and a prepending a `reverse_` to the sorts is even better still.  I'm +1 on this.
Message has been deleted

Kelvin Raffael Stinghen

unread,
Oct 18, 2019, 12:19:34 PM10/18/19
to elixir-l...@googlegroups.com
I’m +1 too. And I can’t think of a better name, since there is already a `Enum.reverse` and a `Enum.sort`, that would be a pretty clear name IMO.

Best,
Kelvin Stinghen
kelvin....@me.com

-- 
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/6384264f-9b2f-4c5d-be5e-54f56d4f79b5%40googlegroups.com.

Mário Guimarães

unread,
Oct 18, 2019, 12:20:22 PM10/18/19
to elixir-l...@googlegroups.com
Hello

for me, the current sort function seems very clear: in the resulting enumeration, the predicate will be true for any two adjacent elements.

iex(14)> 1..10 |> Enum.to_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

iex(15)> Enum.sort(1..10, &>=/2)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

iex(16)> 10..1 |> Enum.to_list
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

iex(17)> Enum.sort(10..1, &<=/2)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

I also do not remember of other programming languages having both a "sort" and "reverse_sort" functions.

With this in mind, I think we do not need yet another function in an already long Enum interface. 

Regards,
Mário


--

Wiebe-Marten Wijnja

unread,
Oct 20, 2019, 2:06:13 AM10/20/19
to elixir-lang-core
Something that came to mind early this morning, is that in the way it is currently defined, the following two things are not equal:

collection
|> Enum.sort(&some_fun/2)
|> Enum.reverse

collection
|> Enum.reverse_sort(&some_fun/2)

The difference arises because of sorting stability: Assuming a stable comparison function, `Enum.sort` is stable. This means that in the first example, elements that are equal end up in the reverse order from their original order.

Because this difference in behaviour might be surprising, maybe we should indeed find a different name for `Enum.reverse_sort`.


José Valim

unread,
Oct 20, 2019, 2:52:30 AM10/20/19
to elixir-l...@googlegroups.com
The other option is to make reverse sort not stable by default, but that doesn’t sound like a good idea.

Maybe other option I can think of is to allow the sorter function to be:

    fun/2 | asc | desc | module | {:asc, module} | {:desc, module}

This way we also solve the issue in that they require slightly different comparisons to solve reverse sorting.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
--

José Valim

unread,
Oct 20, 2019, 3:33:40 AM10/20/19
to elixir-l...@googlegroups.com
A proposal for asc/desc has been sent: https://github.com/elixir-lang/elixir/pull/9432


José Valim
Skype: jv.ptec
Founder and Director of R&D

Reply all
Reply to author
Forward
0 new messages