2

i have a code which is always sorting in ascending order. i need to do it descending based on the input.

I have followed the previous post How to sort Wrapper list?'s code snippet). Here is my code,

public with sharing class OpenSalesOrderWrapper implements Comparable {
    public Integer OrderNo;
    public String PurchaseOrderNo;
    public String SalesOrg;
    public static String sortBy{get;set;}

    //Constructor
    public OpenSalesOrderWrapper(Integer OrdN, String PO, String SO) {
        OrderNo = OrdN;
        PurchaseOrderNo = PO;
        SalesOrg = SO;
    }       

    public Integer compareTo(Object compareTo) 
    {
        System.debug('BBBBBBBBBBBBBBBB'+OrderNo);
        OpenSalesOrderWrapper jobsWrapper = (OpenSalesOrderWrapper) compareTo;
        if(sortBy == 'OrderNoAsc') {
            if (OrderNo > jobsWrapper.OrderNo) {
                return 1;
            }
        } else if(sortBy == 'OrderNoDesc') {
            if(OrderNo < jobsWrapper.OrderNo) {
                return -1;
            }
        }
        return 0;        
    }
}

Class to test the above code:

public with sharing class ComparableTest {
public static void testSort() {
    List<OpenSalesOrderWrapper> jobs = new List<OpenSalesOrderWrapper>();

    jobs.add(new OpenSalesOrderWrapper(5,'E','T'));
    jobs.add(new OpenSalesOrderWrapper(2,'F','Q'));
    jobs.add(new OpenSalesOrderWrapper(1,'U','P'));
    jobs.add(new OpenSalesOrderWrapper(4,'D','S'));
    jobs.add(new OpenSalesOrderWrapper(3,'C','R'));

    System.debug('Before sort:@@@@@@@@@@@'+jobs);
    // Add things to the list ...
    jobs.sort();
    System.debug('After sort:>>>>>>>>>>>'+jobs);

}

}

when i executed as anonymous block by setting the value for static variable as given below,

OpenSalesOrderWrapper.sortby = 'OrderNoAsc';
ComparableTest.testsort();

gives me sorted results based on "OrderNo" 1,2,3,4,5 which is working as expected. But,

OpenSalesOrderWrapper.sortby = 'OrderNoDesc';
ComparableTest.testsort();

does not sort the list in either way. Simply output the same unsorted list. Am i doing anything wrong here? Please suggest.

Thanks, Baskaran

2 Answers 2

4

This should work: you just need to reverse the +1/-1 values to reverse the order:

public Integer compareTo(Object compareTo) 
{
    System.debug('BBBBBBBBBBBBBBBB'+OrderNo);
    Boolean ascending = sortBy == 'OrderNoAsc';
    OpenSalesOrderWrapper jobsWrapper = (OpenSalesOrderWrapper) compareTo;
    if (OrderNo > jobsWrapper.OrderNo) {
        return ascending ? 1 : -1;
    } else if(OrderNo < jobsWrapper.OrderNo) {
        return ascending ? -1 : 1;
    } else {
        return 0;
    }      
}
3
  • Thanks Keith. It works for me. i am wondering on seeing the shorthand form of "Boolean ascending = sortBy == 'OrderNoAsc';" while i am writing it in two to three lines.
    – Bforce
    Commented Apr 16, 2014 at 19:56
  • 1
    @baskaran Well as there are only 2 choices - ascending or descending - using a Boolean would be cleaner than setting a magic String value. But "a == b" evaluates to a Boolean and then the Boolean is being assigned using "=". You could use brackets to make the precedence explicit but Apex follows a fairly typical pattern where assignment is at the bottom so done last - salesforce.com/us/developer/docs/apexcode/Content/….
    – Keith C
    Commented Apr 16, 2014 at 20:10
  • Thanks a lot Keith for the great detailed explanation. I understand it better now.
    – Bforce
    Commented Apr 16, 2014 at 20:14
2

You are returning -1 and +1 if you toggle between > or <, but you are also toggling the > or < signs in the same case, meaning you will get the same result for ascending or descending.

Try changing it to:

public Integer compareTo(Object compareTo){
    System.debug('BBBBBBBBBBBBBBBB'+OrderNo);
    OpenSalesOrderWrapper jobsWrapper = (OpenSalesOrderWrapper) compareTo;
    Integer result = (Order > jobsWrapper.OrderNo) ? 1 : (Order < jobsWrapper.OrderNo ? -1 : 0);
    return sortBy == 'OrderNoAsc' ? result : result * -1;
    return 0;        
}

//Edit, looks like Keith and I have the same answer.

You must log in to answer this question.

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