Need to Sort Map

Bill Norris

Active Member
Licensed User
Longtime User
I am populating a map with two fields from a database. Then, using map.containskey, I am reading two more fields and adding to the map any values from those fields that do not already exist in the map. This is easy to do, but now I want to sort the map based on the key value. I know that List has a sort command, but I need to use a map, since the List only accepts one value per element. What I am considering is, once I have all the values in the map, I will populate a List with the key values from the map. I will then sort the list and build a new map from the sorted list. As I add each key element to the new map, I will get the corresponding value element using Get(key_value). I am sure this will work, but it just seem a bit cumbersome. Might there be a more efficient or elegant way to end up with a sorted Map?
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Someone else posted something similar to this a couple days ago. Seems overkill to pull from a database to a map then from there to another database to sort then back to a map. Would be better to just sort in the SQL when originally pulling from the database into the map. The Map will be stored in the order you insert.

Edit: Just looked and the previous thread was from you, only for Distinct values...
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…