Saturday, 6 December 2014

Wrapper List Sorting(Column Sorting) using Comparable Interface

Comparable Interface adds sorting support for Lists that contain non-primitive types, that is, Lists of user-defined types.

Whenever we want to achieve column sorting in wrapper list then we have to implement Comparable and we should define the "compareTo" method as shown below.

Here i am querying the account object records and passing them to wrapper list for giving example. Basically a wrapper class can contain combination of different types. For all such type of things also it will work.Comparable interface is very useful for sorting wrapper list.


Class:
======

public class AccountClass {

    public List<AccountWrappercls> wrapperList{get;set;}
    public string sortableField{get;set;}
    public string sortOrder{get;set;}
    public string previoussortfield{get;set;}
    public AccountClass(){
        wrapperList = new List<AccountWrappercls>();
        sortableField = 'name';
        sortOrder = 'asc';
        previoussortfield =sortableField;
        for(Account actobj:[select id,name,ActiveDate__c,ACnumber__c from Account where ActiveDate__c!=null and ACnumber__c!=null]){
                AccountWrappercls wrapObj = new AccountWrappercls(actobj.ActiveDate__c,actobj.Acnumber__c,actobj.name);
                wrapperList.add(wrapObj);
        }           
        wrapperList.sort();
    }
    public pagereference doSort(){      
        //sortorder='asc';      
        if(previoussortfield == sortableField){
            sortorder='desc';
            previoussortfield=null;
        }
        else{
            sortorder='asc';            
            previoussortfield=sortableField;
        }
        System.debug('sortableField:::'+sortableField);
        System.debug('sortOrder::'+sortOrder);
        AccountWrappercls.sortableField = sortableField;
        AccountWrappercls.sortorder = sortOrder;        
        wrapperList.sort();
        return null;
    }
}

Class Which Implements Comparable interface:
======================================
public  class AccountWrappercls implements comparable{
  public Date createdDate {get;set;}
  public decimal num1 {get;set;}
  public String  name {get;set;}
  public static String sortableField='name';
  public static String sortOrder='asc';  
  public AccountWrappercls(Date createdDate,decimal num1,String  name){
      this.createdDate = createdDate;
      this.num1 = num1;
      this.name =name;
      //this.sortableField = sortableField;
      //this.sortOrder = sortOrder;
  }
   public  Integer compareTo(Object compareTo) {
     AccountWrappercls compWrap = (AccountWrappercls)compareTo;
     if(sortOrder == 'asc'){
    if(sortableField == 'num1'){     
            if (num1 == compWrap.num1) return 0;
          if (num1 > compWrap.num1) return 1;
          return -1;        
    }
    if(sortableField == 'name'){     
            if (name == compWrap.name) return 0;
          if (name > compWrap.name) return 1;
          return -1;        
    }
    if(sortableField == 'createdDate'){     
            if (createdDate == compWrap.createdDate) return 0;
          if (createdDate > compWrap.createdDate) return 1;
          return -1;        
    }}else{
      if(sortableField == 'num1'){     
            if (num1 == compWrap.num1) return 0;
          if (num1 > compWrap.num1) return -1;
          return 1;        
    }
    if(sortableField == 'name'){     
            if (name == compWrap.name) return 0;
          if (name > compWrap.name) return -1;
          return 1;        
    }
    if(sortableField == 'createdDate'){     
            if (createdDate == compWrap.createdDate) return 0;
          if (createdDate > compWrap.createdDate) return -1;
          return 1;        
    }
    }
    
    return null;
    }    
    
}
Visualforce Page:
==============
<apex:page controller="AccountClass">
 <apex:form >
  <apex:pageBlock id="pb">
   <apex:pageBlocKTable value="{!wrapperList}" var="wrapObj">
    <apex:column >
     <apex:facet name="header">
      <apex:commandLink value="Name {!IF(sortableField=='name',IF(sortOrder='asc','▼','▲'),'')}" action="{!doSort}" rerender="pb">
       <apex:param name="name" value="name" assignTo="{!sortableField}"></apex:param>
      </apex:commandLink>
     </apex:facet>
     
      <apex:outputText value="{!wrapObj.name}"/>
     
    </apex:column>
    <apex:column >
     <apex:facet name="header">
      <apex:commandLink value="Ac Number {!IF(sortableField=='num1',IF(sortOrder='asc','▼','▲'),'')}" action="{!doSort}" rerender="pb">
        <apex:param name="num1" value="num1" assignTo="{!sortableField}"></apex:param>
      </apex:commandLink>
     </apex:facet>
     
      <apex:outputText value="{!wrapObj.num1}"/>
         
    </apex:column>
    <apex:column >
     <apex:facet name="header">
      <apex:commandLink value="Date {!IF(sortableField=='createdDate',IF(sortOrder='asc','▼','▲'),'')}" action="{!doSort}" rerender="pb">
       <apex:param name="createdDate" value="createdDate" assignTo="{!sortableField}"></apex:param>
      </apex:commandLink>
     </apex:facet>
     
      <apex:outputText value="{0, date, MM/d/yyyy}">
       <apex:param value="{!wrapObj.createdDate}"/>
      </apex:outputText>
        
    </apex:column>
   </apex:pageBlocKTable>
  </apex:pageBlock>
 </apex:form>
</apex:page>                                        


                                        
Output:
======


2 comments:

  1. static can only be used on fields of a top level type.
    Please help.

    ReplyDelete
    Replies
    1. Hi Suman,

      Static variables should be used on fields of a top level only.

      Here Wrapper class which we are using is a seperate class not innner class.
      Write it as seperate class as shown above then your problem will be resolved.

      Thanks,
      Balaji M

      Delete

Best Practices for Building Prompt Templates

 Hi, The following best practices should be considered when building prompt templates: Make sure that your prompt templates are concise and ...