anusha(salesforce developer)

How to Sort Wrapper class Collection in Apex

How to Sort Wrapper class Collection in Apex

There is inbuilt functionality in Apex to sort the primitive datatypes supported by force.com. I am sure maximum of developers must have come across the situations where they need to sort custom datatype built by them i.e. custom class or I would say wrapper class in terms of force.com.
But think, How force.com will come to know that how to sort class? In case of integer, date or double they already know. But how they are going to know that what is going to be content in your class? Before Summer’12 there was no direct way. However thanks to Salesforce to introduce the interface Comparable in Summer’12 release. And… there is smile on the face of Java developers, because they already know what it is.
So let’s start with “What is the use of interface Comparable?“ I would say, this is the way to tell force.com that how we are going to compare the custom Objects (Wrapper class).
Comparable Interface:
To implement the Comparable Interface, you must declare global class with the implements keyword as follow:
1global class Book implements Comparable
Now, your class must implement the method “compareTo“ as follow:
1global  Integer compareTo(Object objToCompare)
2{
3    //Your code to implement logic
4}
compareTo() method:
The compareTo() method must return following integer value
  1. 0 if both object is equal
  2. >0 if the instance object is greater than “objToCompare”
  3. <0 if the instance object is smaller than “objToCompare”
Sample code
Let’s take the example of class “Book”, which have following data members,
  • BookTitle – String
  • Author – String
  • TotalPages – Integer
  • Price – Double
1global class Book implements Comparable {
2 
3    public String BookTitle ;
4    public String Author ;
5    public Integer TotalPages ;
6    public Double Price ;
7    public Date publishingDate;
8 
9    public enum SORT_BY {
10        ByTitle,ByPage
11        }
12 
13    //Variable to decide the member on which sorting should be performed
14    public static SORT_BY sortBy = SORT_BY.ByTitle;
15 
16    public Book(String bt, String a, Integer tp, Double p, Date pd)
17    {
18        BookTitle = bt;
19        Author = a;
20        TotalPages = tp;
21        Price = p;
22        publishingDate = pd;
23    }
24 
25    global Integer compareTo(Object objToCompare) {
26        //Sort by BookName Alphabetically
27        if(sortBy == SORT_BY.ByTitle)
28        {
29            return BookTitle.compareTo(((Book)objToCompare).BookTitle);
30        }
31        else //Sort by Book price
32        {
33            return Integer.valueOf(Price - ((Book)objToCompare).Price);
34        }
35    }
36}
As you can see in above code, we are sorting on the basis of either BookTitle or Price. The logic can be anything depending on business requirement.
Running the sample code in Developer Console:
Run below code in “Developer Console” from browser or “Execute Anonymous” from Eclipse.
1Book[] books = new Book[]{
2    new Book('Salesforce Handbook','Jeff Douglas',360,35,Date.newInstance(20110320)),
3    new Book('Let Us C','Yashavant P. Kanetkar',593,58,Date.newInstance(20080321)),
4    new Book('Head First Design Patterns ','Elisabeth Freeman',678,28,Date.newInstance(200411,01))
5};
6 
7Book.sortBy = Book.SORT_BY.ByTitle;
8books.sort();
9System.debug(books);
10 
11Book.sortBy = Book.SORT_BY.ByPrice;
12books.sort();
13System.debug(books);
When you will see the output, first time it sorted by BookTitle and second time it is sorted according Price.

No comments:

Post a Comment