function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
aks0011aks0011 

How to sort a map<string,list<string>> ???

CheyneCheyne
Technically, you cannot sort a Map. You can, however, sort a list, so you can make a sorted list of map keys. This technique is described in this post: https://developer.salesforce.com/forums/ForumsMain?id=906F00000008yqnIAA

It might look something like this:

Map<String, List<String>> myMap = new Map<String, List<String>>();
List<Sring> myList1 = new List<String>{'1', '2', '3'};
List<String> myList2 = new List<String>{'4', '5', '6'};
myMap.put('1', myList1);
myMap.put('2', myList2);

//Construct a list from the map keyset and sort it
List<String> keyList = new List<String>();
keyList.addAll(myMap.keySet());
keyList.sort();

//Loop through the sorted keys
for (String key : keyList) {
    List<String> value = myMap.get(key);
}
praveen murugesanpraveen murugesan
Hi,

You can't sort a map but there is a one way to store a sorted value in Map.

Eg.

Map<String, List<String>> myMap = new Map<String, List<String>>();
myMap = [select ------ ----- --- from obj order by name];

So query will return a ordered value based on Name.

Thanks
aks0011aks0011
hey thansk for your answer


but I used wrapper class , fixed.