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
swain 10swain 10 

Sorting a Map on descending order basis of one score field(decimal)

 Map<Id,decimal> allResultMap = new Map<Id,decimal>();

I need to sort in descending order on the basis of the score field which is of type decimal.
NagendraNagendra (Salesforce Developers) 
Hi Swain,

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. 

Sorting on map is not possible. But if you want to sort map with there key then we can do something like this :
 

Public class mapsort
{
    public mapsort()
    {

        map<String , Integer> accMap = new Map<String,Integer>();
        accMap.put('Id2',2);
        accMap.put('Id1',1);
        accMap.put('Id4',4);
        accMap.put('Id3',3);
        accMap.put('Id5',5);

        List<String> aList = new List<String>();
        aList.addAll(accMap.keySet());
        aList.sort();
        //so here you will get sorted total base on key
        for(String a: aList)
        {
           System.debug( '::::::::::::: ' + accMap.get(a)) ;
        }
    }

}
Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra