Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wouldn't it be nice if Timex.compare/2 were compatible with Enum.sort? #699

Open
maxmarcon opened this issue Jan 5, 2022 · 0 comments
Open

Comments

@maxmarcon
Copy link

I would have expected to be able to sort the result of Timex.parse!/2 by using the Timex.compare/2 function, so I don't have to worry about whether I'm dealing with a Date or a NaiveDateTime.

iex> ["2020-01-06", "2020-01-05", "2019-12-31"] 
|> Enum.map(& Timex.parse!(&1, "{ISOdate}")) 
|> Enum.sort(Timex)

[~N[2020-01-06 00:00:00], ~N[2020-01-05 00:00:00], ~N[2019-12-31 00:00:00]] <-- not the right order 😢

Expected:

[~N[2019-12-31 00:00:00], ~N[2020-01-05 00:00:00], ~N[2020-01-06 00:00:00]]

Of course, this happens because Timex.compare/2 returns -1, 0, 1 as opposed to :lt, :eg, :gt as needed by Enum.sort/2.

Any thoughts on this?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant