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

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement