2

I need to sort Wrapper List by two values(Quantity and Price). The below code Sorts List only by Quantity.How to sort list by both Quantity and Price.

public Class SortByTwoValues implements Comparable
{
    public Decimal Quantity{get; set;}
    public Decimal Price{get; set;}
    public String ItemName{get;set;}

    public Integer compareTo(Object compareTo) 
    {
        SortByTwoValues compareToRec = (SortByTwoValues)compareTo;
        if (Quantity == compareToRec.Quantity) 
            return 0;
        if (Quantity > compareToRec.Quantity) 
            return 1;
        return -1;        
    }    
}

Thanks in advance

2 Answers 2

11

You have to pick which field takes priority and only when that field is the same consider the second field.

So assuming you want to sort by quantity first and price second:

public Integer compareTo(Object compareTo) {
    SortByTwoValues compareToRec = (SortByTwoValues)compareTo;
    if (Quantity > compareToRec.Quantity) {
        return 1;
    } else if (Quantity < compareToRec.Quantity) {
        return -1;
    } else {
        // First value the same, consider second value
        if (Price > compareToRec.Price) {
            return 1;
        } else if (Price < compareToRec.Price) {
            return -1;
        } else {
            return 0;
        }          
    }       
}  
6
  • Thanks for the response.If i want sort Separately means sort only by Quantity or only by Price or both.Is there any way to do this. Aug 28, 2014 at 8:33
  • @NullReferenceException Not entirely sure what you mean... You could introduce flags on the wrapper to vary what compareTo does. Please explain more.
    – Keith C
    Aug 28, 2014 at 8:41
  • I have a pageblocktable with 3 columns,Name,Quantity and Price,first i will show only Quantity and Name by Sorting the List based on Quantity.and i have a button GetPrice after clicking i have to show Price in PageblockTable, now what happening is the wrapper list already sorted by Quantity,but Price is not in order.I want to display Price also in sorted order. Aug 28, 2014 at 9:18
  • @NullReferenceException I think the posted answer should do that.
    – Keith C
    Aug 28, 2014 at 9:29
  • @KeithC: If its TEXT and not numbers (price/quantity) how to do the sorting? Please comment.
    – sfdcFanBoy
    Sep 2, 2015 at 10:43
-1

Another, & simpler, way to sort on multiple fields in Apex is to write your data to a table used to store data temporarily & then simply retrieve the same data again in a list using SOQL & use the 'order by' clause to sort the way you want.

The temporary data in the table can then be deleted using the same list you retrieved. You will still have the list to work with.

3
  • to many moves, as for me. Jan 31, 2018 at 9:47
  • Shouldn't be unless you have lots of variables but yopu don't have to write all of them to the table if you don't want to. It should also be quicker to code than the above code, especially once you go to sorting on 3 or more variables and have set up the table. Its also simpler to read the code and understand it. Feb 1, 2018 at 1:44
  • Here is the code in my example that does the actual sorting ----------------> : list<Temp_Misc_Data__c> tempMiscDataList = [select id, var01__c, var02__c, var03__c, var04__c, var05__c, var06__c, var07__c, var08__c, var09__c, from Temp_Misc_data__c where var01__c =: hashString order by var03__c asc, var09__c asc, var04__c asc, var08__c asc]; Feb 1, 2018 at 1:48

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.