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

2 Answers 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
    Commented Mar 29, 2017 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
    Commented Mar 29, 2017 at 16:47
  • Good point, but thats an anti-pattern and the above will fix the problem posed. Commented Mar 29, 2017 at 16:50
  • Hi, I have tried adding this but it doesn't seem to make any difference? thanks
    – realtek
    Commented Mar 29, 2017 at 18:49
  • Can you put in your whole code base including where you invoke the sort? Commented Mar 29, 2017 at 21:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .