+ Start a Discussion

Comparable Interface for Objects inside a Wrapper Class


I have a Wrapper Class which has two object type's declared inside it which stored in it.

I'm then adding an instance of this class to a list. The reason I had to do this is because there is a polymorphic field in the ProcessWorkItem object and I needed to loop through all records to get the related information.

The problem is, I cannot get the Comparable Interface working on these objects in order to sort the list accordingly.

The code executes without errors but the list is not sorted.

I've triggering this by using myList.sort();
public Class MyWrapper implements Comparable {
        public ProcessInstanceWorkitem obj1 {get; set;}
        public MySalesforceObject obj2 {get; set;}
        public Integer compareTo(Object o)
            MyWrapper comparemyWrp = (MyWrapper)(o);
            if (SORT_BY == ACTOR_SORT) {
                return sortByActor(comparemyWrp);
            return 0;
        private Integer sortByActor(MyWrapper wrap) {
            if (this.obj2.CreatedDate > wrap.obj2.CreatedDate) {
                return 1;
            if (this.obj2.CreatedDate == wrap.obj2.CreatedDate) {
                return 0;
            return -1;

Any help would be great
Ravi Dutt SharmaRavi Dutt Sharma
I fear you can compare date time fields like this: his.obj2.CreatedDate > wrap.obj2.CreatedDate
Hi Ravi - thanks for your response. That was actually a test, and I am really tying:
this.obj2.Actor.Name > wrap.obj2.Actor.Name
this.obj1.Actor.Name > wrap.obj1.Actor.Name

I meant!