Salesforce Apex

Sometimes we need to Sort Apex Map to built functionality in apex but  Map do not has  sorting support, they are a key value pair collection.

 To Sort Apex Map we need to  use wrapper class and comparable interface.

before going to the code lets know about concept of wrapper class in apex.

  What is Wrapper Class :

Wrapper Class is collection of two or more diffrent objects.
So these are the abstract data structure by which a developer create its own data structure by groupuing another available  data structure.
It expands user capablity and give more power to developer a customized application. it can be user for creating Visualforce page where page contains Boolean as check box and list item as value.

Here in this post I explain you how to sort a map by both key and values .

1) So  first we are creating a map that is  regionContactMap.  having key as city and value as count

 

Map regionContactMap = new Map();
regionContactMap.put('noida',5);
regionContactMap.put('delhi',2);
regionContactMap.put('hapur',8);
regionContactMap.put('ghaziabad',7);

2) Now  creating a list of type RegionWrapper  and adding data to the list like below :

RegionWrapper[] regionList = new RegionWrapper[0];
for(string key: regionContactMap.keyset()) {
regionList.add(new RegionWrapper(key, regionContactMap.get(key)));
}

// sorting list in ascending order
regionList.sort();

// final list of key value pair (in  descending order)

List finalList = new List();
for(Integer i = regionList.size()-1; i>=0;i–)
{
finalList.add(regionList.get(i));
}

system.debug(‘value of region list is @@@@@’+regionList);
system.debug(‘value of region list in descending order @@@@@’+finalList);

3) Creating a wrapper class  named Regionwrapper that  implement Comparable Interface . Wrapper class   data members name should be  map keys and values.

 

// wrapper class to sort map

public class RegionWrapper implements Comparable {
public string region;
public Integer counter;
public RegionWrapper(string region, Integer counter) {
this.region = region;
this.counter = counter;
}

public Integer compareTo(Object other) {
return counter-((RegionWrapper)other).counter;
}
}

 

Now here is the final code you just need to replace your map in the below code the code will return you a finalist of your sorted map.

 

Map regionContactMap = new Map();
regionContactMap.put('noida',5);
regionContactMap.put('delhi',2);
regionContactMap.put('hapur',8);
regionContactMap.put('ghaziabad',7);

RegionWrapper[] regionList = new RegionWrapper[0];
for(string key: regionContactMap.keyset())
{
  regionList.add(new RegionWrapper(key, regionContactMap.get(key)));
}

// sorting list in ascending order 
regionList.sort();

// final list of key value pair (in  descending order)

List finalList = new List();
for(Integer i = regionList.size()-1; i>=0;i--)
{
  finalList.add(regionList.get(i));
}

system.debug('value of region list is @@@@@'+regionList);

// here final list contains sorted map values 
system.debug('value of region list in descending order @@@@@'+finalList);

// wrapper class to sort map


public class RegionWrapper implements Comparable 
{
  public string region;
  public Integer counter;
  public RegionWrapper(string region, Integer counter) 
  {
    this.region = region;
    this.counter = counter;
  }

  public Integer compareTo(Object other) {
    // for descending order
  return counter-((RegionWrapper)other).counter;
  }
}

NOTE:  To Sort map in ascending order  replace the code in compareto  like:

return ((RegionWrapper)other).counter – counter;

 

 

Hits: 4358

Share Post

By Himanshu Rana

My Name is Himanshu Rana, 23 Years young, born and grow up in Ghaziabad, India. A High Spirited Salesforce Admin, Developer and a Blogger. I currently work at Wakencode Technologies,

One thought on “Sort Apex Map Using Wrapper Class”

Leave a Reply

Your email address will not be published. Required fields are marked *