silviurosu

silviurosu

Enum.sort does not work as expected

I have a situation that I can not figure out yet for some time and it drives me nuts. Either Enum.sort is not working as expected either I am doing it wrong.
I have some rules have a name and depend on each other. Source is the name of the dependency. I am trying to sort them ascending by dependency to be able to use them later to compute some amounts:

rules = [
  %{
    name:  "ambassador1",
    source: "ambassadors"
  },
  %{
    name: "zuppler fee",
    source: "total"
  },
  %{
    name:  "ambassadors",
    source: "zuppler fee"
  }
]

sorter = fn
  sorter = fn
  %{name: name1}, %{source: source2} when name1 == source2 -> true
  %{source: source1}, %{name: name2} when source1 == name2 -> false
  %{name: name1, source: source1}, %{name: name2, source: source2} when name1 == name2 and source1 == source2 -> true
  %{name: name1, source: source1}, %{name: name2, source: source2} -> name1 < name2 and source1 < source2
end
  _, _ -> false
end
rules |> Enum.sort(sorter) |> IO.inspect()

Result is:

[
  %{name: "ambassadors", source: "zuppler fee"},
  %{name: "zuppler fee", source: "total"},
  %{name: "ambassador1", source: "ambassadors"}
]

when the expected result would be something like:

[
  %{name: "zuppler fee", source: "total"},
  %{name: "ambassadors", source: "zuppler fee"},
  %{name: "ambassador1", source: "ambassadors"}
]

Can somebody give me a hint?

Marked As Solved

al2o3cr

al2o3cr

Calling sorter with %{name: "zuppler fee", source: "total"} and %{name: "ambassador1", source: "ambassadors"} is going to return false, so I don’t think Enum.sort would find the configuration you’re looking for. The position of %{name: "ambassador1", source: "ambassadors"} in the result depends on the existence of %{name: "ambassadors", source: "zuppler fee"}

Consider topological sort + tree traversal algorithms for this one.

Also Liked

LostKobrakai

LostKobrakai

Also souce in your map keys vs. source in your pattern matches.

Last Post!

ondrej-tucek

ondrej-tucek

@silviurosu Is this what you want?

Enum.sort_by(rules, &(&1.name), :desc)

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement