# Sort Wrapper List by two values

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;
}
}
``````

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;
}
}
}
``````
• 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 '14 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. Aug 28 '14 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 '14 at 9:18
• @NullReferenceException I think the posted answer should do that. Aug 28 '14 at 9:29
• @KeithC: If its TEXT and not numbers (price/quantity) how to do the sorting? Please comment. Sep 2 '15 at 10:43

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.

• to many moves, as for me. Jan 31 '18 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 '18 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 '18 at 1:48