Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule MultisortDecimalTest do
- use ExUnit.Case
- @data [
- %{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(400.0)},
- %{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(500.0)},
- %{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(600.0)},
- %{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(700.0)}
- ]
- @expected [
- %{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(500.0)},
- %{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(400.0)},
- %{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(700.0)},
- %{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(600.0)}
- ]
- def compare(d1, d2) do
- case Decimal.cmp(d1, d2) do
- :gt -> 1.0
- :eq -> 0.0
- :lt -> -1.0
- end
- end
- def sort(data) do
- data
- |> Enum.sort_by(fn row ->
- {to_string(row.multiplier), to_string(row.points)}
- end)
- |> Enum.reverse()
- end
- test "works" do
- assert sort(@data) == @expected
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement