shahryarjb

shahryarjb

How to improve sort of maps in a list which have duplicate key

Hello friends,
I have a JSON file in which I save some information about my apps and their dependencies, so some of my app dependencies may be the same, and I need to merge them, but my priority is the version, so I should reject the lower version and keep the higher version.

[
    {"app":"testi","dependencies":[{"app":"phoenix","min":"3.7","max":"4"},{"app":"phoenix_live_view","max":"0.17.7","min":"4.01.0"},{"app":"ueberauth","max":"0.17.7","min":"1.17.7"},{"app":"ueberauth_github","min":"0.8.1"},{"app":"ueberauth_google","min":"0.10.1"}],"dependency_type":"soft_update","git_tag":null,"timeout":null,"type":"hex","update_server":null,"url":"https://hex.pm/packages/mishka_social","version":"0.0.2 "},
    {"app":"testi2","dependencies":[{"app":"phoenix","min":"1.6"},{"app":"phoenix_live_view","max":"0.1.0","min":"0.01.78"},{"app":"ueberauth","max":"0.17.7","min":"0.17.7"},{"app":"ueberauth_github","min":"0.8.1"},{"app":"ueberauth_google","min":"0.10.1"},{"app":"test","max":"0.10.25","min":"0.10.1"}],"dependency_type":"soft_update","git_tag":null,"timeout":null,"type":"hex","update_server":null,"url":"https://hex.pm/packages/mishka_social","version":"0.0.2 "}
]

I convert top JSON with Jason to a list and get dependencies key, the output

[
  [
    %{"app" => "phoenix", "max" => "4", "min" => "3.7"},
    %{"app" => "phoenix_live_view", "max" => "0.17.7", "min" => "4.01.0"},
    %{"app" => "ueberauth", "max" => "0.17.7", "min" => "1.17.7"},
    %{"app" => "ueberauth_github", "min" => "0.8.1"},
    %{"app" => "ueberauth_google", "min" => "0.10.1"}
  ],
  [
    %{"app" => "phoenix", "min" => "1.6"},
    %{"app" => "phoenix_live_view", "max" => "0.1.0", "min" => "0.01.78"},
    %{"app" => "ueberauth", "max" => "0.17.7", "min" => "0.17.7"},
    %{"app" => "ueberauth_github", "min" => "0.8.1"},
    %{"app" => "ueberauth_google", "min" => "0.10.1"},
    %{"app" => "test", "max" => "0.10.25", "min" => "0.10.1"}
  ]
]

As you see, some app are the same, but the versions are not and maybe are.

For example

we have 2 times phoenix, the first one min (it should be sorted by min, this key is important in version) is 3.7 and the second is 1.6, so I need to keep

%{"app" => "phoenix", "max" => "4", "min" => "3.7"},

and reject the others

my code:

Enum.map(json_data, &(&1["dependencies"]))
|> Enum.concat
|> Enum.group_by(&(&1["app"]))
|> Map.to_list()
|> Enum.map(fn {_key, list} ->
    Enum.sort_by(list, &(&1["min"]))
    |> List.last()
end)

I think i’m in a wrong way, do you have any suggestions?

This is the final output

[
  %{"app" => "phoenix", "max" => "4", "min" => "3.7"},
  %{"app" => "phoenix_live_view", "max" => "0.17.7", "min" => "4.01.0"},
  %{"app" => "test", "max" => "0.10.25", "min" => "0.10.1"},
  %{"app" => "ueberauth", "max" => "0.17.7", "min" => "1.17.7"},
  %{"app" => "ueberauth_github", "min" => "0.8.1"},
  %{"app" => "ueberauth_google", "min" => "0.10.1"}
]

By the way, maybe I have another apps which have empty dependencies.

Thank you in advance.

Marked As Solved

LostKobrakai

LostKobrakai

If you only need the max you can use Enum.max_by. I though you need both ends.

Enum.map + Enum.concat could just be Enum.flat_map. Also no need for the Map.to_list. Maps are also enumerables.

Also Liked

LostKobrakai

LostKobrakai

Enum.min_max_by(["3.0.0", "1.5.0", "3.0.0-rc.1"], &Function.identity/1, Version)
# {"1.5.0", "3.0.0"}
LostKobrakai

LostKobrakai

This could ne just & &1["min"]

LostKobrakai

LostKobrakai

It uses Version.compare to determine order and the min/max from that.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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 49522 488
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement