Become a Salesforce Platform Developer

Master the Art of Building Custom Apps and Solutions Start Your Platform Developer Journey

Sorting Lists in Salesforce

By Amelia Brewin

Sorting objects is a requirement that comes up from time to time. It can be as simple as ordering by a simple number field (such as Opportunities priority) or more complex ordering involving multiple fields.

Luckily for us Salesforce has the Comparable Interface. With a handy wrapper class that implements Comparable and takes our object in the constructor we can define our own ordering.

This list is being displayed in a Visualforce table however you could easily adapt it to update a field or other requirements. In this example I am sorting a List of Contacts to determine the order to call them, my sorting criteria are:

  1. Do Not Call is not selected.
  2. The contact has a phone number in either Phone, Mobile Phone or Other Phone fields.
  3. Those in the Finance department are higher.
  4. Created date is higher.

Step 1:

Create the controller with the wrapper class, the wrapper constructor will take a contact object. In the controller constructor we will query the contacts and create a list of contact wrappers that will be sorted and passed to the Visualforce page.

Step 2

The contact wrapper needs variables of all the values we will be using to sort. In the constructor we will assign the values of the from the contact to these variables. In our case we also want a boolean variable this will represent contacts that have a number in at least one of the phone number fields.

Step 3:

Now is time to set our sorting requirements. To do this we override the compareTo method from the Comparble interface. This needs to return a number:

  • 0 if the records are equal.
  • 1 if the record is greater than the comparison record.
  • -1 if record is less than the comparison record.

Our first criteria is Do Not Call, if both the current record and the one being compared to are the same we return 0, if the current record is true (they don’t want to be called) and the compared to is false (they do want to be called) we want the current record to move above so return -1. And vice versa if the current record is false (they want to be called) and the compared to is true (they don’t want to be called) we want the current record to move below so return 1.

The variable phonePresent is also a boolean that we have created in the constructor of contact wrapper. It represents a contact that has a number in one of the phone fields.

So we want those contacts where do not call is the same to then use the secondary sorting criteria of phonePresent, therefore where we previously returned 0 add in the condition about phonePresent.


If you want simply to order alphabetically you can use the operators:

Or you can give specific strings, in our case I want those in the Finance department to be at the top of the list:

Dates and Numbers

Dates and numbers can are very easy to order using the standard comparison operators. We just need to check if they’re equal, greater than or less than.

The completed wrapper:

Step 4:

Once we have the list of our ContactWrappers all we have to do to sort is call .sort() on the list. This rearranges all the records in the list and we can use however we need. We could:

Iterate over them and assigning the index to a variable such as priority eg:

Display them in a Visualforce table in the sorted order eg:

The Result:

In the screenshot below, the Finance contacts are highlighted. The first group have a phone number, and the Do Not Call flag is not selected, so these are listed first. Notice how the most recently created contact is listed first. Contacts from other departments are then listed, that have a phone number. Then comes another Finance contact, as even though it doesn’t have a phone number, the contacts in the Finance department are ordered higher than other departments if all else is the same. The last Finance contact is second last, as the Do Not Call flag is ticked.


Complete Code:


  1. GeorgeP

    Another way to sort on multiple fields in APex is to write your data to a table used to store data temporarily and then simply retrieve the same data again in a list using SOQL and use the ‘order by’ clause to sort the way you want.
    The temporary data in the table can then be deleted using the same list you retrieved. You will still have the list to work with.