You need to sign in to do that
Don't have an account?

Sorting objects in list according to othe fields
Hi
I need to Sort entries of quoteLineItemList by PriceBookEntry.Product2.Product_Family__r.Sort_Order__c and PriceBookEntry.Product2.ProductCode and then store the sorted list in orderedLineItem.
How can I do this using apex code.
I need to Sort entries of quoteLineItemList by PriceBookEntry.Product2.Product_Family__r.Sort_Order__c and PriceBookEntry.Product2.ProductCode and then store the sorted list in orderedLineItem.
How can I do this using apex code.
Map<String,QuoteLineItem> productFamilyMap = new Map<String,QuoteLineItem>();
Map<String,QuoteLineItem> productFamilyMapSorted = new Map<String,QuoteLineItem>();
List<String> orderingList = new List<String>();
List<QuoteLineItem> sortedQuoteLineItemList = new List<QuoteLineItem>();
for (QuoteLineItem qliObj: quoteLineItemList){
productFamilyMap.put(qliObj.PriceBookEntry.Product2.Product_Family__r.Sort_Order__c,qliObj);
}
//Add all elements of Set into a list
orderingList.addAll(productFamilyMap.keyset());
//Sort the List
orderingList.sort();
for ( Integer i= 0;i < orderingList.size(); i++){
productFamilyMapSorted.put(orderingList[i],productFamilyMap.get(orderingList[i]));
}
//Get the sorted list
sortedQuoteLineItemList = productFamilyMapSorted.values();
Kindly ignore syntax errors, if any.
Regards
Medhya
All Answers
1. Sort while querying the data. Use ORDER BY clause and directly store sorted results in the list.
SELECT ...... FROM .... WHERE ......
ORDER BY PriceBookEntry.Product2.Product_Family__r.Sort_Order__c ,PriceBookEntry.Product2.ProductCode
See Link : http://developer.force.com/cookbook/recipe/sorting-query-results
2. Another possible way is to add the data to a Map and sort by keyset.
Add the keyset to a list using addAll
Sort the list using list.sort() method
Then iterate over this list and get values from you map.
See Link : https://developer.salesforce.com/forums/?id=906F00000008z7zIAA
3. Third possible way is to custom sort.
See Link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_list_sorting_sobject.htm
However the 2nd and 3rd way will let you sort the list by only one of the fields. Therefore the first approach will be suitable in your case.
I don't think salesforce allows sorting by two fields at once in a standard way.
If this helps, mark it as solved.
Regards
Medhya Mahajan
My list contains data from two places i.e. 1 from select statement and another using for loop as below. I have already collected the items in the list, just need to sort it by PriceBookEntry.Product2.Product_Family__r.Sort_Order__c and PriceBookEntry.Product2.ProductCode.
quoteLineItemList = [select Id,ListPrice,Discount,
Duration_Number__c , Quantity, Quantity__c, TotalPrice, UnitPrice, Description, Duration__c,
PriceBookEntryId, PriceBookEntry.Name, PriceBookEntry.IsActive,
PriceBookEntry.Product2Id, PriceBookEntry.Product2.Name, PriceBookEntry.UnitPrice,
PriceBookEntry.PriceBook2Id, PriceBookEntry.Product2.Product_Family__r.Name,
Sales_Price_After_Discount__c, Discount_On_List_Price__c,
PriceBookEntry.Product2.Requires_Support_Fees__c,
PriceBookEntry.Product2.Product_Family__r.Product_Family__c,PriceBookEntry.Product2.Product_Family__r.Sort_Order__c,
PriceBookEntry.Product2.Maximum_Discount__c,
PriceBookEntry.Product2.Maximum_Discount_Without_Approval__c
from QuoteLineItem
where QuoteId=:currQuote.Id and PriceBookEntry.Product2.Name != 'Support Fees'
order by PriceBookEntry.Product2.Product_Family__r.Sort_Order__c, PriceBookEntry.Product2.ProductCode];
for(Zero_Quantity_LineItems__c zqli : zqList) {
system.debug('zqli:'+zqli);
QuoteLineItem qli = new QuoteLineItem(PriceBookEntry = pbeMap.get(zqli.PriceBookEntryID__c),
PriceBookEntryId = zqli.PriceBookEntryID__c, QuoteId = currQuote.Id,
Discount = 0, UnitPrice = pbeMap.get(zqli.PriceBookEntryID__c).UnitPrice,
Duration__c = ''+zqli.Number_Of_Month__c, Quantity__c = 0);
quoteLineItemList.add(qli);
}
Hope this helps.
Regards
Medhya Mahajan
I'm new to salesforce, hence can you please write down the code for this and send it.
Thanks in advance.
Can you please help me on this.
Arun
We can sort by only one field at a time I guess.
Medhya Mahajan
Map<String,QuoteLineItem> productFamilyMap = new Map<String,QuoteLineItem>();
Map<String,QuoteLineItem> productFamilyMapSorted = new Map<String,QuoteLineItem>();
List<String> orderingList = new List<String>();
List<QuoteLineItem> sortedQuoteLineItemList = new List<QuoteLineItem>();
for (QuoteLineItem qliObj: quoteLineItemList){
productFamilyMap.put(qliObj.PriceBookEntry.Product2.Product_Family__r.Sort_Order__c,qliObj);
}
//Add all elements of Set into a list
orderingList.addAll(productFamilyMap.keyset());
//Sort the List
orderingList.sort();
for ( Integer i= 0;i < orderingList.size(); i++){
productFamilyMapSorted.put(orderingList[i],productFamilyMap.get(orderingList[i]));
}
//Get the sorted list
sortedQuoteLineItemList = productFamilyMapSorted.values();
Kindly ignore syntax errors, if any.
Regards
Medhya