Using Comparable Interface to sort by Date

I have come across the requirement in which I have to sort the list of Apex Class based on Date.

If you want to add sorting support for Lists that contain non-primitive types, that is, lists of user-defined types. you must implement the Comparable interface with its compareTo method in your class.

To implement the Comparable interface, you must first declare a class with the implements keyword.
For sorting by date, we can use daysBetween(Date) which returns the number of days between that called the method and specified date.Code Snippet of Apex Class:Sorting list of Apex Class and binded to VF page:
Visuaforce Page:If you notice the list is sorted by Date.Feel free to post comments if you have any questions. Cheers !!!

Author

wahib

Leave a comment

Your email address will not be published. Required fields are marked *

× Let's Chat!