Hi,
currently I’m stuck at the Elixir’s track Boutique Inventory in Elixir on Exercism practice. I can’t manage to make the tests for the sort_by_price
function pass. What confuses me is with which result the test fails.
My current implementation looks like this:
def sort_by_price(inventory) do
sortFn = fn(a,b) ->
if a["price"] == b["price"] do
false
else
a["price"] < b["price"]
end
end
Enum.sort(inventory, sortFn)
end
The failing test looks like this:
test sort_by_price/1 the order of items of equal price is preserved (BoutiqueInventoryTest)
test/boutique_inventory_test.exs:24
Assertion with == failed
code: assert BoutiqueInventory.sort_by_price([
%{price: 65, name: "Maxi Yellow Summer Dress", quantity_by_size: %{}},
%{price: 60, name: "Cream Linen Pants", quantity_by_size: %{}},
%{price: 33, name: "Straw Hat", quantity_by_size: %{}},
%{price: 60, name: "Brown Linen Pants", quantity_by_size: %{}}
]) == [
%{price: 33, name: "Straw Hat", quantity_by_size: %{}},
%{price: 60, name: "Cream Linen Pants", quantity_by_size: %{}},
%{price: 60, name: "Brown Linen Pants", quantity_by_size: %{}},
%{price: 65, name: "Maxi Yellow Summer Dress", quantity_by_size: %{}}
]
left: [%{name: "Brown Linen Pants", price: 60, quantity_by_size: %{}}, %{name: "Straw Hat", price: 33, quantity_by_size: %{}}, %{name: "Cream Linen Pants", price: 60, quantity_by_size: %{}}, %{name: "Maxi Yellow Summer Dress", price: 65, quantity_by_size: %{}}]
right: [%{name: "Straw Hat", price: 33, quantity_by_size: %{}}, %{name: "Cream Linen Pants", price: 60, quantity_by_size: %{}}, %{name: "Brown Linen Pants", price: 60, quantity_by_size: %{}}, %{name: "Maxi Yellow Summer Dress", price: 65, quantity_by_size: %{}}]
stacktrace:
test/boutique_inventory_test.exs:25: (test)
* test with_missing_price/1 works for an empty inventory (0.00ms) [L#41]
* test with_missing_price/1 filters out items that do have a price (0.00ms) [L#46]
There are two things that confuse me:
- if I change the
false
in theif
block totrue
, I have an additional test failing. I don’t understand why because in the documentation of thesort
function it says " The given function should compare two arguments, and returntrue
if the first argument precedes or is in the same place as the second one.", so in my opinion returningtrue
would be the correct behaviour in case of equality. - I would understand if the sort was not stable, i.e. the two 60-price-items swapped. But I don’t understand how it is possible that my result is not sorted correctly at all (33, 60, 60, 65)
Thank you very much for your help!