+ Start a Discussion
ArunaAruna 

Map descending and ascending sorting on vf page when user clicks on command link --> urgent help me

Hi is anyone got the solution to this sorting problem.

I am unable to do descending order sorting.
My requirement is I have map Of <Id,Name>  when user click on the link the map values need to sort either ascending or descending order

Example If the Map values contain
Map.put(id1,Name1);  
Map.put(id3,Name3);  
Map.put(id2,Name2);  
Map.put(id5,Name5);  
Map.put(id6,Name6);  
Map.put(id4,Name4);  

Result for ascending 
Map.put(id1,Name1);  
Map.put(id2,Name2);  
Map.put(id3,Name3);  
Map.put(id4,Name4);  
Map.put(id5,Name5);  
Map.put(id6,Name6); 

Result for desendoing\

Map.put(id6,Name6);  
Map.put(id5,Name5);  
Map.put(id4,Name4);  
Map.put(id3,Name3);  
Map.put(id2,Name2);  
Map.put(id1,Name1); 

I did use different methods to solve this but nothing is working me out. Please, anyone, help me out I need to solve this issue.
One ve page I need to display these values.

Thank you
Aruna
Raj VakatiRaj Vakati
Map<String, String> storedMap = new Map<String, String>{'c'=>'Value3', 'a'=>'Value1', 'd'=>'Value4', 'b'=>'Value2'};

Map<String, String> temoMap = new Map<String, String>();


for(String key: getSortedKeyset(storedMap)) {
 //   system.debug('Result: '+storedMap.get(key));
temoMap.addAll(storedMap ,storedMap.get(key));
}

public  List<String> getSortedKeyset(Map<String, String> dataMap) {
    List<String> keySetList = new List<String>();
    keySetList.addAll(dataMap.keySet());
    keySetList.sort();
    return keySetList;
}

storedMap.addAll(temoMap);

 
ArunaAruna
getting errors there no addAll method in MAP. I did use putAll method but getting errors
Raj VakatiRaj Vakati
Sorry its not addAll its putall 


storedMap.putAll(temoMap);
Raj VakatiRaj Vakati
Map<String, String> storedMap = new Map<String, String>{'c'=>'Value3', 'a'=>'Value1', 'd'=>'Value4', 'b'=>'Value2'};

Map<String, String> temoMap = new Map<String, String>();


for(String key: getSortedKeyset(storedMap)) {
 //   system.debug('Result: '+storedMap.get(key));
temoMap.addAll(storedMap ,storedMap.get(key));
}

public  List<String> getSortedKeyset(Map<String, String> dataMap) {
    List<String> keySetList = new List<String>();
    keySetList.addAll(dataMap.keySet());
    keySetList.sort();
    return keySetList;
}

storedMap.putAll(temoMap);

 
ArunaAruna
Hi Raj thank you for your response I appreciate your help.

Below line of code is in correct 
temoMap.addAll(storedMap ,storedMap.get(key));
I did trued using 
temoMap.putAll(storedMap ,storedMap.get(key)); but getting error .
Can you please post me an executed version of the code