[SalesForce] How to sort the values inPageblockTable vf page

In vf page i have created look up field (look up to custom object "PO") and created a "Add" custom button. When the user click on the show button all the Serial record names are displayed in Page block table.I need to sort this record based on their names (Eg: 1001, 1002, 1003…….). In the code i have sort() but the records are not showing in order.

enter image description here

Apex :

set<id> MapOfwrapperPO2 = new set<Id>();
  public PageReference addPO(){
    showSection5=true;
    List<Keycode__c> poLineItems = [Select Id,Name,PO__c,PO__r.Name,PO__r.Container_Number__c,PO__r.Event_Number__c,PO__r.Source_of_Supply__c,PO__r.Booking_Date_Time__c,PO__r.Order_Type__c,PO__r.Season_Code__c,PO__r.Number_Pallets_Cartons__c from Keycode__c  where id IN: selectedPoId ORDER By Name DESC  ];     
    for(Keycode__c poLineItem:poLineItems){
      if(!MapOfwrapperPO2.contains(poLineItem.id)){
      wrapperPO wsinglePO = new wrapperPO(nextIdent++);
      wsinglePO.poLineItemId = poLineItem.Id;
      wsinglePO.poLineItemName = poLineItem.Name;
      wsinglePO.poID = poLineItem.PO__c;
      wsinglePO.poName = poLineItem.PO__r.Name;
      wsinglePO.poContainer=poLineItem.PO__r.Container_Number__c;
      wsinglePO.poEventnumber =poLineItem.PO__r.Event_Number__c;
        wsinglePO.poSourceSupply=poLineItem.PO__r.Source_of_Supply__c;       
      wsinglePO.poOtype=poLineItem.PO__r.Order_Type__c; 
      wsinglePO.poSeasonCode=poLineItem.PO__r.Season_Code__c; 
      wsinglePO.poNumberPallets=poLineItem.PO__r.Number_Pallets_Cartons__c;
       MapOfwrapperPO2.add(poLineItem.Id);
     wPO.add(wsinglePO);

      }
    }
       return null;
  }

Vf code:

<apex:pageBlockSection id="poLineItemdetails" rendered="{!showSection5}" >
        <apex:pageBlockTable value="{!wPO}" var="poLine" id="newItems" >        
        <apex:column value="{!poLine.poName}" title="PO Number" headerValue="PO Number"/>
         <apex:column value="{!poLine.poContainer}" title="Container Number" headerValue="Container Number" />        
        <apex:column value="{!poLine.poEventnumber}" title="Event Number" headerValue="Event Number" />
        <apex:column value="{!poLine.poSourceSupply}" title="Source Supply" headerValue="Source Supply" />
        <apex:column value="{!poLine.poOtype}" title="Order Type" headerValue="Order Type" />
        <apex:column value="{!poLine.poSeasonCode}" title="Season Code" headerValue="Season Code" />        
        <apex:column value="{!poLine.poNumberPallets}" title="Number Pallets" headerValue="Number Pallets" />
        <apex:column value="{!poLine.poLineItemName}" title="PO LineItem" headerValue="PO LineItem" />
        <apex:column headerValue="Action">
            <apex:commandButton value="Delete" action="{!delWrapper}" rerender="newItems">
               <apex:param name="toDelIdent" value="{!poLine.ident}" assignTo="{!toDelIdent}"/> 
            </apex:commandButton>
         </apex:column>       
        </apex:pageBlockTable>
     </apex:pageBlockSection>
<apex:commandButton value="Add PO" action="{!addPO}" reRender="block" />

Best Answer

Use please comparable interface in your wrapper class.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_list_sorting_sobject.htm

This should solve all your problems.

Related Topic