Skip to content

Instantly share code, notes, and snippets.

@elbow-jason
Last active September 7, 2019 16:33
Show Gist options
  • Save elbow-jason/8da58de1a8c804c4c0189e2dd136f32e to your computer and use it in GitHub Desktop.
Save elbow-jason/8da58de1a8c804c4c0189e2dd136f32e to your computer and use it in GitHub Desktop.
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
@elbow-jason
Copy link
Author

...but I can't seem to edit the gist...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment