+ Start a Discussion

Sorting in Map



I want to sort Map.In Map i have Account type of key and class.

eg. public Map<Account,Total>totalMap;


i want to sort in basis of my class data my class looks like-: 

Public class Total


private sum{get;set}




i want only 5 records in Assending order.


Please help!!!!



Map can't be sort. 

but if you want key wise sorted data then you can do something like this


Map<Account, Total> map = new Map<Account, Total>();


List<Acount> aList = new List<Acount>();



//so here you will get sorted total base on account

for(Account a: aList){

 Total t = map.get(a);



First of all in map you can put only primitive type of value as a key. And inbuilt sorting for map is not supported. You need to do it manually using loop on the key.