[Proposal] Enum.sort for Dates

475 views
Skip to first unread message

Josh Crews

unread,
Jan 20, 2017, 2:39:17 PM1/20/17
to elixir-lang-core

Current behavior:


iex(3)> [~D[2017-01-04], ~D[2017-04-03], ~D[2017-01-05]] |> Enum.sort

[~D[2017-04-03], ~D[2017-01-04], ~D[2017-01-05]]


Proposed:

iex(3)> [~D[2017-01-04], ~D[2017-04-03], ~D[2017-01-05]] |> Enum.sort

[~D[2017-01-04], ~D[2017-01-05], ~D[2017-04-03]]


What I'm currently doing is:

iex(4)> [~D[2017-01-04], ~D[2017-04-03], ~D[2017-01-05]] |> Enum.map(&(Date.to_erl/1)) |> Enum.sort |> Enum.map(&(Date.from_erl!/1))

[~D[2017-01-04], ~D[2017-01-05], ~D[2017-04-03]]


which is working great, but would be cool to have Enum.sort work on Date types too.

Paul Schoenfelder

unread,
Jan 20, 2017, 2:42:20 PM1/20/17
to elixir-l...@googlegroups.com
Probably easier to use `Enum.sort_by(dates, &DateTime.to_unix/1)`, no?

Paul

--
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-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/5cb8a9f9-ee03-497e-87d6-7bd9633b2f78%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Josh Crews

unread,
Jan 20, 2017, 3:03:40 PM1/20/17
to elixir-lang-core
Hi. I love Timex so much!

Regarding work around... I guess?

What do you think of the main thread question, "should Enum.sort work on Date types"?


On Friday, January 20, 2017 at 1:42:20 PM UTC-6, Paul Schoenfelder wrote:
Probably easier to use `Enum.sort_by(dates, &DateTime.to_unix/1)`, no?

Paul
On Fri, Jan 20, 2017 at 1:39 PM, Josh Crews <crews...@gmail.com> wrote:

Current behavior:


iex(3)> [~D[2017-01-04], ~D[2017-04-03], ~D[2017-01-05]] |> Enum.sort

[~D[2017-04-03], ~D[2017-01-04], ~D[2017-01-05]]


Proposed:

iex(3)> [~D[2017-01-04], ~D[2017-04-03], ~D[2017-01-05]] |> Enum.sort

[~D[2017-01-04], ~D[2017-01-05], ~D[2017-04-03]]


What I'm currently doing is:

iex(4)> [~D[2017-01-04], ~D[2017-04-03], ~D[2017-01-05]] |> Enum.map(&(Date.to_erl/1)) |> Enum.sort |> Enum.map(&(Date.from_erl!/1))

[~D[2017-01-04], ~D[2017-01-05], ~D[2017-04-03]]


which is working great, but would be cool to have Enum.sort work on Date types too.

--
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,
Jan 20, 2017, 3:05:39 PM1/20/17
to elixir-l...@googlegroups.com
Sorting in Elixir is based on the structure so there is no way Enum.sort can be made to work for DateTime. You need to give a sorter or an operation, like Paul described, if you would like to change its behaviour.



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

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8a996168-b548-4730-a2a6-baa38a88d712%40googlegroups.com.

Paul Schoenfelder

unread,
Jan 20, 2017, 3:12:37 PM1/20/17
to elixir-l...@googlegroups.com
Sorry Josh, I should have replied to your original proposal. What I was getting at is that you can already use the existing Enum sorting functions with Dates, just by providing a sorter function, you don't have to convert to/from. As José mentioned though, there isn't any way to make Enum.sort/1 itself work on structs, it's up to the struct implementor to define functions which can be easily used with `Enum.sort_by` to make them sortable, such as `DateTime.to_unix`. There have been previous conversations on the mailing list about this subject, but I don't believe any of them got to a point where there was a viable implementation.

Paul


To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8a996168-b548-4730-a2a6-baa38a88d712%40googlegroups.com.

Ben Wilson

unread,
Jan 21, 2017, 9:54:20 AM1/21/17
to elixir-lang-core
See an earlier discussion on a similar idea: https://groups.google.com/forum/#!topic/elixir-lang-core/eE_mMWKdVYY
Reply all
Reply to author
Forward
0 new messages