Ajax (1) Apex Class (12) Apex Trigger (2) Community (2) Home Page (1) HTML (4) Integration (3) JS (7) KB (1) Label (1) Licenses (1) Listing (1) Log (1) OOPs (5) Sharing (1) Static Resource (1) Test Class (3) URI (1) Visualforce (10)

Thursday 9 April 2015

Wrapper Class Sorting

public class WrapperClass implements Comparable {

    public Contact idm;
    public String name;
    public String phone;
    
    // Constructor
    public WrapperClass (Contact i, String n, String p) {
        idm = i;
        name = n;
        phone = p;
    }
    
    // Implement the compareTo() method
    public  Integer compareTo(Object compareTo) {
        WrapperClass  compareToWarp = (WrapperClass)compareTo;
        if (idm.lastname == compareToWarp.idm.lastname) return 0;
        if (idm.lastname > compareToWarp.idm.lastname) return 1;
        return -1;        
    }
}

-----------------------------
public class WrapperSorting
{
  
  public WrapperSorting()
  {
  List<WrapperClass> wrapList=new List<WrapperClass>();
        wrapList.add(new WrapperClass(new Contact(lastname='jo jo'),'Joe Smith', '4155551212'));
        wrapList.add(new WrapperClass(new Contact(lastname='Zo jo'),'J. Smith', '4155551212'));
        wrapList.add(new WrapperClass(new Contact(lastname='Ko jo'),'Caragh Smith', '4155551000'));
        wrapList.add(new WrapperClass(new Contact(lastname='Po jo'),'Mario Ruiz', '4155551099'));
        wrapList.sort();
        for(WrapperClass obj:wrapList)
        {
         System.debug('Test='+obj);
        }
  }


}

No comments:

Post a Comment