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.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement