I have a Customer Roster which is a visualforce page. Every new customer is added to the Roster using the Visualforce Page. Now, we want to display the list in such a way that the recently added customer should be displayed first. What would be the best way to achieve that? My code is as follows:

Visualforce Page:

<apex:page controller="CustomerDataController">
   <apex:form >
    Customer Name: <apex:inputText value="{!Names}" />
    Customer Address: <apex:inputText value="{!Addresses}" />
    <apex:commandButton value="Add" action="{!AddCustomerInfo}"/>
    <table border="0">
         <td>Customer Name</td>
          <td>Customer Address</td>
         <td> Entered Date </td>
    <apex:repeat value="{!wrapper.customerData}" var="cust">
        <td> <apex:outputText value="{!cust.Name}"/></td>
        <td> <apex:outputText value="{!cust.Address}"/></td> 
        <td> <apex:outputText value="{!cust.DateCreated}"/></td></tr>

Apex Class:

public class CustomerDataController{

    public String Names { get; set; }
    public WrapperClass wrapper{get; set;}

    public CustomerDataController(){
     if(wrapper== null)
                wrapper= new WrapperClass ();
    public void AddCustomerInfo() {
    WrapperClass.CustomerInformation addCustomer = new WrapperClass.CustomerInformation();
       addCustomer.Name = Names;
       addCustomer.Address= Addresses;
       addCustomer.DateCreated = Date.today();  
       //wrapper.customerData.sort(); //Received Exception: List is not Comparable 

You can define how the items in a list sort by implementing the Comparable interface.

It sounds like you want to order by the created date/time in descending order. So if you make sure that Datetime.now() is used for the newly added entries and the standard SObject CreatedDate is used for ones pulled from the database (in a field on the class) then code something like this should result in the ordering you need:

public class WrapperClass {
    public class CustomerInformation implements Comparable {
        // Populate using Datetime.now() or the CreatedDate of the SObject:
        private Datetime created;
        // Descending
        public Integer compareTo(Object o) {
            CustomerInformation that = (CustomerInformation) o;
            if (that.created < this.created) return -1;
            } else if (that.created > this.created) return 1;
            } else return 0;


From the code you just added, it looks like you may have been adding the Comparable to the WrapperClass rather than to the CustomerInformation class where it is needed because it is instances of the latter that are in the list.

  • Thank You @KeithC for your quick rsponse and for pointing out my mistake. It definitely helped. – user10665 Jul 27 '14 at 20:56

You will need to add implements comparable to your wrapper...

you forgot this line: obj_wrapper compareToFW = (obj_wrapper)compareTo;

See below as an example

public with sharing class obj_wrapper implements comparable{

    public Account a;
    public date created_date;

        public field_wrapper(Account a){
             this.a = a;
             created_date = a;

        public Integer compareTo(Object compareTo) {
            obj_wrapper compareToFW = (obj_wrapper)compareTo;

            if (created_date == compareToFW. created_date) return 0;
            if (created_date > compareToFW. created_date) return 1;
            return -1;        


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.