kamaroly
How to Control Map Key Order in Elixir for Custom Report Formatting?
Elixir is sorting map by key name in ascending order, but I don’t want this behaviour. It is causing me to have undesired reports output. I’d like to have the employee number and names to begin, then followed by leave types instead of the following.
Is there a way to rearrange the map by key in Elixir, so that
records = [
%{
"Annual Leave" => "0",
"Compassionate" => "0",
"Maternity" => "0",
"Paternity" => "0",
"Paternity Leave" => "0",
"Sick Leave" => 0,
"Study Leave" => 0,
"emp_no" => "EMP0004",
"names" => "Benard Kipucho"
},
%{
"Annual Leave" => "87",
"Compassionate" => 0,
"Maternity" => 0,
"Paternity" => 0,
"Paternity Leave" => "1024",
"Sick Leave" => "100",
"Study Leave" => "989",
"emp_no" => "EMP001",
"names" => "Kamaro Lambert"
}
]
# SOMETHING LIKE THIS CAN make the map starts with emp_no and names.
Enum.map_key_starts_with(records, ["emp_no", "names"])
The above should give the following results
[
%{
"emp_no" => "EMP0004", # Shifted to the starting point
"names" => "Benard Kipucho", # Shifted to the starting point
"Annual Leave" => "0",
"Compassionate" => "0",
"Maternity" => "0",
"Paternity" => "0",
"Paternity Leave" => "0",
"Sick Leave" => 0,
"Study Leave" => 0
},
%{
"emp_no" => "EMP001", # Shifted to the starting point
"names" => "Kamaro Lambert",# Shifted to the starting point
"Annual Leave" => "87",
"Compassionate" => 0,
"Maternity" => 0,
"Paternity" => 0,
"Paternity Leave" => "1024",
"Sick Leave" => "100",
"Study Leave" => "989"
}
]
Marked As Solved
kamaroly
Thank you all for jumping on this issue. I truly appreciate.
I had to write a Report module that:
- Aceepts a list of records, and a list of keys in order I want.
- Loops throuch each record and map it with
Map.get(record, ordered_key, "")
records = [
%{
"Annual Leave" => Decimal.new("21"),
"Compassionate" => Decimal.new("14"),
"emp_no" => "EMP001",
"names" => "Jane Doe"
},
%{
"Annual Leave" => Decimal.new("10"),
"Compassionate" => Decimal.new("5"),
"emp_no" => "EMP002",
"names" => "John Smith"
}
]
Report.format(records, ["names", "emp_no", "Annual Leave", "Compassionate"])
Gives
[
%{
"names" => "Jane Doe",
"emp_no" => "EMP001",
"Annual Leave" => 21,
"Compassionate" => 14,
},
%{
"names" => "John Smith",
"emp_no" => "EMP002",
"Annual Leave" => 10,
"Compassionate" => 5,
}
]
Also Liked
rvirding
To make matters worse (?
) the ordering of elements in maps is completely undefined and changes depending on the number of elements. It use to be that for less than 32 elements the ordering was term ordering and for more elements it was “random” depending on the internal search algorithm used.
The thing to remember is that the actual ordering is purely internal. There are iterator functions which can give you some control of which order you want to step over the elements.
LostKobrakai
Maps are an unordered dataformat. The BEAM just has a default sort order given it has a global ordering between any term it can represent, which is used to order things where they need to become ordered. So the solution here is to use a datatype which does retain order (lists) instead of maps.
For your usecase I’d even argue that this is a pure view layer concern – as in how data is presented – so your view layer code could simply order the columns as needed before rendering the data.
NobbZ
Have you considered using structs with a custom inspection protocol implementation?
Maps with string keys deep into the program always smell.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









