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"
}
]