Enum.sort_by/3 crashes in production

I wrote this simple line code while the code was running in a development environment.

r variable is just an Ecto schema retrieved by a query, I just wanted to get the value of the field offers (a lis that contains another Ecto schema) and just sort it by updated_date.

And this works fine… in development.

r |> Map.get(:offers) |> Enum.sort_by(&(&1.updated_at), NaiveDateTime)

When I build the project in a production environment an exception is raised in that line code returning this error.

 ** (exit) an exception was raised:
     ** (BadFunctionError) expected a function, got: NaiveDateTime
         (elixir) lib/enum.ex:2354: anonymous fn/3 in Enum.sort_by/3
         (stdlib) lists.erl:969: :lists.sort/2
         (elixir) lib/enum.ex:2354: Enum.sort_by/3

This is weird because I don’t find any problem with that code. I tried with other solutions but the same error happens.

r |> Map.get(:offers, []) |> Enum.sort_by(&(&1.updated_at), {:asc, NaiveDateTime})
r |> Map.get(:offers, []) |> Enum.sort_by(fn o -> o.updated_at end, NaiveDateTime)
r |> Map.get(:offers, []) |> Enum.sort_by(fn o -> o.updated_at end, {:asc, NaiveDateTime})

I’m running out of ideas, you guys know why this error happens?

What elixir version are you running in prod and which at dev?

Passing in modules is a recent feature from 1.9 or 1.10.

1 Like

1.10 to be exact: https://github.com/elixir-lang/elixir/releases/tag/v1.10.0

1 Like

I think I got my answer…
In dev I’m running 1.10.3 and prod is running 1.9.4. I thought I was running the same version… thanks!

1 Like