Showing posts with label sorting List. Show all posts
Showing posts with label sorting List. Show all posts

Wednesday, March 28, 2012

Sort an Apex class Object List

[Edit : Now there is an interface natively provided by salesforce to sort the class object lists]

I recently needed to sort an apex class object List which was retrieved through a webservice callout as it could not be sorted at the service end.

This is the apex method which was used to build it

 public void testsort(){
        map<string,List<accClass>> stracc = new map<string,list<accClass>>();
        for(accClass a:accClassobj ){
            if(stracc.containsKey(a.name))
                stracc.get(a.name).add(a);           
            else
              stracc.put(a.name,new accclass[]{a});          
        }
        accclassobj= new list<accclass>();
        list<string> nameList = new list<string>();
        nameList.addAll(stracc.keyset());
        nameList.sort();       
        for(string s:nameList){
           accclassobj.addAll(stracc.get(s));          
        }

Steps involved in sorting

1. build a map with object parameter as the key by which we gonna sort and List<object> as the value.
2. sort the keyset
3. arrange the values based on the sorted keyset, so that object gets sorted by that parameter

It is a map of parameter and list of object but not parameter and just object to handle the records which will have same parameters. This sorting handles null values automatically. This sorting can never be dynamic and you will need to create more methods based on the object parameter by which sorting is needed.

Friday, March 23, 2012

Filter list alphabetically



There was a requirement where i needed to build a list which can be filtered based on the starting alphabet. And also the List needs to remember the selections made as we move through other pages.

<apex:page controller="Pagin_alpha">
<style type="text/css">
      .loadingIcon {
            background-image: url(/img/loading.gif);
            width: 16px;
            height: 16px;
        }
     </style>
<script type="text/javascript">
function checkAll(cb,cbid)
        {
            var inputElem = document.getElementsByTagName("input");                     
            for(var i=0; i<inputElem.length; i++)
            {  
               
                 if(inputElem[i].id.indexOf(cbid)!=-1){                                        
                inputElem[i].checked = cb.checked;
                }
            }
        }
</script>
<apex:form id="form">
<br/>
<div style="align:right;">
<apex:repeat value="{!alphabet}" var="a">
<apex:commandLink value="{!a}" action="{!refreshList2}" rerender="form" style="{!if($CurrentPage.parameters.alpha=a,'font-weight:bold','')}" status="stat">
<apex:param name="alpha" value="{!a}"/>
</apex:commandLink>
&nbsp;|&nbsp;
</apex:repeat>
</div>
<br/>
<apex:pageBlock id="block">
<apex:pageBlockButtons >
<apex:commandButton rendered="{!standardsetcon.hasprevious}" value="Previous" action="{!standardsetcon.previous}" rerender="block,block2" status="stat"/>
<apex:commandButton rendered="{!standardsetcon.hasnext}" value="Next" action="{!standardsetcon.next}" rerender="block,block2" status="stat"/>
</apex:pageBlockButtons>
<apex:actionStatus id="stat">
<apex:facet name="start">
<apex:outputPanel layout="block" styleClass="message infoM4">
<apex:panelGrid columns="2" styleClass="messageTable" columnClasses="messageCell" style="padding:0px;margin:0px;">
<apex:panelGroup >
<img class="loadingIcon" src="/s.gif"/>
</apex:panelGroup>
<apex:panelGroup >
<div class="messageText">Please wait...</div>
</apex:panelGroup>
</apex:panelGrid>
</apex:outputPanel>
</apex:facet>
<apex:facet name="stop">
<apex:pageBlockTable value="{!CurrentList}" var="c" id="table">
<apex:column >
<apex:facet name="header"><apex:inputcheckbox onclick="checkAll(this,'check')" value="{!fals}" /></apex:facet>
<apex:inputcheckbox value="{!c.checked}" id="check">
</apex:inputcheckbox>
</apex:column>
<apex:column value="{!c.ocontact.name}" headerValue="Name"/>
</apex:pageBlockTable>
</apex:facet>
</apex:actionStatus>
</apex:pageBlock>
<apex:pageBlock title="Selected Contacts" id="block2">
<apex:commandButton value="Display Selected" rerender="block2"/>
<apex:pageBlockTable value="{!DisplaySelectedList}" var="c">
<apex:column value="{!c.name}" headerValue="Name"/>
<apex:column value="{!c.phone}" headerValue="Phone"/>
</apex:pageBlockTable>
</apex:pageBlock>

</apex:form> 
</apex:page>

In the VF page i am displaying the List 'CurrentLsit' which would hold the contact list with checkbox which is paginated and 'DisplaySelectedList' which would remember the selected contact. Array Alphabet is used in repeat to build the link of alphabets at the top of page

Controller method



public with sharing class Pagin_alpha {

    
    private List<contactSet> contactSetList{get;set;}
    private string contactListQuery;
    private set<contact> selectedContact;
   
    public List<string> alphabet{get;set;}
    public boolean fals{get;set;}  
   
    public Pagin_alpha(){
      fals=false;
      alphabet=new string[]{'All','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; 
      contactSetList = new List<contactSet>();
      selectedContact = new set<contact>();
      contactListQuery= 'select id,name,phone from contact limit 1000';   
    }
   
     Private void updateSelectedContact(){
        for(contactSet cs:contactSetList ){
           if(cs.checked)
               selectedcontact.add(cs.ocontact);
           else{
               if(selectedcontact.contains(cs.ocontact))
                   selectedcontact.remove(cs.ocontact);
               }
       }  
    }
   
      public ApexPages.StandardSetController standardSetCon {
        get {
            if(standardSetCon == null) {
                standardSetCon = new ApexPages.StandardSetController(Database.getQueryLocator(contactListQuery));
                // sets the number of records in each page set
                standardSetCon .setPageSize(20);
            }
            return standardSetCon ;
        }
        set;
    }
 
    public List<contactSet> getCurrentList() {
       updateSelectedContact();
       contactSetList = new List<contactSet>();      
        for (contact category : (List<contact>)standardSetCon.getRecords()){       
            if(selectedContact.contains(category))         
            contactSetList.add(new contactSet(category,'true'));
            else
            contactSetList.add(new contactSet(category));
        }
        fals=false;
        return contactSetList;
    }
   
    public PageReference refreshList2() {      
       standardSetCon = null;    
       string s;
       if(apexpages.currentpage().getparameters().get('alpha') == 'All')
           s='%';
       else
           s= apexpages.currentpage().getparameters().get('alpha')+'%';
      
       contactListQuery= 'select id,name,phone from contact where name like' +'\''+s +'\''+ 'limit 5000';          
        return null;
    }
   
  
   
    public List<contact> getDisplaySelectedList(){
        updateSelectedContact();
        List<contact> displaycon = new list<contact>();
        displaycon.addall(selectedcontact);
        return displaycon;
    }
      
    public class contactSet {
        public Boolean checked{ get; set; }
        public contact ocontact { get; set;}

        public contactSet(){
            ocontact = new contact();
            checked = false;
        }
        public contactSet(contact c){
            ocontact = c;
            checked = false;

        }
        public contactSet(contact c,string s){
            ocontact = c;
            checked = true;

        }
    }
   
 
}
getCurrentList populates the currentList in the page. refreshList2 will refresh the list when the link on top of the page to filter the contact is clicked.

This concept can be extended to multiple fields in the List .