You need to sign in to do that
Don't have an account?
Gtemporao
Sorting a Nested Map
Hello,
I have a data structure of the type Map<String,Map<String,Decimal>>, which relates each customer to a list of bank accounts and their respective balances, as well as a total balance which would be the sum of all balances.
A sample line of this structure is something like the following:
"John" -> ("Bank 1" -> 1200.00, "Bank 2" -> 300.50, "Total" -> 1500.50)
I would like to sort this structure by the "Total" value. Can you please give me some ideas?
Thank you!
Even if you sort the map somehow by total the map will be always sorted by default on "Keys"
Lets say you have
"John" -> ("Bank 1" -> 1200.00, "Bank 2" -> 300.50, "Total" -> 1500.50)
"Brian" -> ("Bank 1" -> 1200.00, "Bank 2" -> 300.50, "Total" -> 1100.50)
"Aaron" -> ("Bank 1" -> 1200.00, "Bank 2" -> 300.50, "Total" -> 1900.50)
lets say you want to sort ascending on total and you would expect Brian, John and Aaron but the map will always have Aaron, Brian and John.. Its sorted by default.
Here is the sample code for your structure sorting and see if it helps... Run it in Developer console and see the logs
All Answers
Even if you sort the map somehow by total the map will be always sorted by default on "Keys"
Lets say you have
"John" -> ("Bank 1" -> 1200.00, "Bank 2" -> 300.50, "Total" -> 1500.50)
"Brian" -> ("Bank 1" -> 1200.00, "Bank 2" -> 300.50, "Total" -> 1100.50)
"Aaron" -> ("Bank 1" -> 1200.00, "Bank 2" -> 300.50, "Total" -> 1900.50)
lets say you want to sort ascending on total and you would expect Brian, John and Aaron but the map will always have Aaron, Brian and John.. Its sorted by default.
Here is the sample code for your structure sorting and see if it helps... Run it in Developer console and see the logs
@Sam_SFDC15: Thank you very, very much. This has not entirely solved my problem but it was enough so I could take over from where you left. I really don't understand why Apex does not allow custom sorting of maps ...