function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arun Yadav 13Arun Yadav 13 

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.


 
Best Answer chosen by Arun Yadav 13
Medhya MahajanMedhya Mahajan
//Assuming PriceBookEntry.Product2.Product_Family__r.Sort_Order__c is of type String
 
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

Medhya MahajanMedhya Mahajan
Listing here 3 possible ways :

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
Arun Yadav 13Arun Yadav 13
Hi Medhya,

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);
           }
                                   
Medhya MahajanMedhya Mahajan
You could try this:
  1. Add the values of the quoteLineItemList to a map and sort the map by keeping one of the fields as keyset( ie. PriceBookEntry.Product2.Product_Family__r.Sort_Order__c) .
  2. Then sort the result map from above by the second field (ie.PriceBookEntry.Product2.ProductCode).

Hope this helps.

Regards
Medhya Mahajan


 
Arun Yadav 13Arun Yadav 13
Hi Medhya,

I'm new to salesforce, hence can you please write down the code for this and send it.

Thanks in advance.
Arun Yadav 13Arun Yadav 13
Hi Medhya

Can you please help me on this.

Arun
Medhya MahajanMedhya Mahajan
Arun, 

We can sort by only one field at a time I guess.

Medhya Mahajan
Arun Yadav 13Arun Yadav 13
OK. Can you please provide me the code to sort it by only one field.
Medhya MahajanMedhya Mahajan
//Assuming PriceBookEntry.Product2.Product_Family__r.Sort_Order__c is of type String
 
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
 
This was selected as the best answer
Arun Yadav 13Arun Yadav 13
Thanks Medhya