Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. defmodule MultisortDecimalTest do
  2. use ExUnit.Case
  3.  
  4. @data [
  5. %{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(400.0)},
  6. %{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(500.0)},
  7. %{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(600.0)},
  8. %{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(700.0)}
  9. ]
  10.  
  11. @expected [
  12. %{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(500.0)},
  13. %{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(400.0)},
  14. %{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(700.0)},
  15. %{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(600.0)}
  16. ]
  17.  
  18. def compare(d1, d2) do
  19. case Decimal.cmp(d1, d2) do
  20. :gt -> 1.0
  21. :eq -> 0.0
  22. :lt -> -1.0
  23. end
  24. end
  25.  
  26. def sort(data) do
  27. data
  28. |> Enum.sort_by(fn row ->
  29. {to_string(row.multiplier), to_string(row.points)}
  30. end)
  31. |> Enum.reverse()
  32. end
  33.  
  34. test "works" do
  35. assert sort(@data) == @expected
  36. end
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement