Activity Forums Salesforce® Discussions Sorting in Map

  • Gourav

    Member
    June 2, 2016 at 1:39 pm

    Map do not hae sorting supported, they are a key value pair collection. Now for your issue you need a sorting algo to work and create a list of key according to the order you want. You can use linear search algo or bubble sort from data structure. Please ask if any issue. Or you can use below method to sort map keys.

    Map<String, String> storedMap = new Map<String, String>{'c'=>'Value3', 'a'=>'Value1', 'd'=>'Value4', 'b'=>'Value2'};
    for(String key: getSortedKeyset(storedMap)) {
    system.debug('Result: '+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;
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos