0

I am trying to implement the comparable interface but I cannot get it to work, nothing seems to make any difference with the ordering of the lists.

 public Class MyWrapper implements Comparable {
        public ProcessInstanceWorkitem obj1 {get; set;}
        public myCustomObject obj2 {get; set;}

        public Integer compareTo(Object o)
        {
            MyWrapper comparemyWrp = (MyWrapper)(o);

            if (SORT_BY == ACTOR_SORT) {
                return sortByActor(comparemyWrp);
            }
            return 0;
        }
        public Integer sortByActor(MyWrapper wrap) {
            if (this.obj1.Actor.Name > wrap.obj1.Actor.Name) {
                return 1;
            }

            if (this.obj1.Actor.Name == wrap.obj1.Actor.Name) {
                return 0;
            }

            return -1;
        }
    }

=== update ===

Here is the code where I am invoking the sort:

I am not sure if I need to call getPendingApprovalList(); in the below code or not but I have tried commenting it out.

 public static String ACTOR_SORT = 'obj1.Actor.Name';  
 public static String SORT_BY = ACTOR_SORT;

And

public PageReference SortbyApprover() {
    SORT_BY = ACTOR_SORT;
    myWrappers = getPendingApprovalList();
    myWrappers.sort();
    //system.debug(myWrappers);
    return null;
}
2
0

It is not clear what values have SORT_BY and ACTOR_SORT and if they are even defined, I would suggest just to remove or comment out lines which are referencing those two undefined variables.

 public Class MyWrapper implements Comparable {
    public ProcessInstanceWorkitem obj1 {get; set;}
    public myCustomObject obj2 {get; set;}

    public Integer compareTo(Object o)
    {
        MyWrapper comparemyWrp = (MyWrapper)(o);

        //if (SORT_BY == ACTOR_SORT) {
            return sortByActor(comparemyWrp);
        //}
        //return 0;
    }
    public Integer sortByActor(MyWrapper wrap) {
        if (this.obj1.Actor.Name > wrap.obj1.Actor.Name) {
            return 1;
        }

        if (this.obj1.Actor.Name == wrap.obj1.Actor.Name) {
            return 0;
        }

        return -1;
    }
}
1
  • Thanks but I commented these out and still no luck. I'm at a loss with this one! They are in fact defined at the top of my class and i've tried hard coding them to always be the correct value also. No joy
    – realtek
    Mar 29 '17 at 18:44
0

You have no constructor so the defaullt constructor never actually instantiates obj1 with a value. As a result sortByActor always returns -1.

Add something like this to your code and it should work:

public myWrapper(Object o){
     this.obj1 = (ProcessInstanceWorkitem) o;
}
4
  • Not necessarily an issue as the properties could be set directly in calling code. A constructor with parameters is not required to populate the properties
    – Eric
    Mar 29 '17 at 16:47
  • Good point, but thats an anti-pattern and the above will fix the problem posed. Mar 29 '17 at 16:50
  • Hi, I have tried adding this but it doesn't seem to make any difference? thanks
    – realtek
    Mar 29 '17 at 18:49
  • Can you put in your whole code base including where you invoke the sort? Mar 29 '17 at 21:39

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.