+ Start a Discussion
ShaTShaT 

Sorting in Map

Hi,

 

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!!!!

 

DharmeshDharmesh

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>();

aList.addAll(map.keySet());

aList.sort();

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

for(Account a: aList){

 Total t = map.get(a);

}

Pradeep_NavatarPradeep_Navatar

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.