3

Am working with Aggregation on an object and i have specified count field in Order By Clause. Am loading aggregate results into wrapper class. Here am not getting the values in specified order. In wrapper class am generating some string to display in visual force page. I want to sort wrapper based on that string value.

   Global class QuoteViewPrint {

public string QuoteID{Get; set;}
public list<QuoteLineItem> QLI{get; set;}
public list<QuoteLineItem> QLIAggr{get; set;}
public list<QLWrap> lstWrap{get; set;}  
public string accName{get; set;}
public string accPhone{get; set;} 
public string MobPhone{get; set;} 
public string conEmail{get; set;}
public string QuoteNo{get; set;}
public string conLastName{get; set;}
public string conFulName{get; set;}
public string accBilStrt{get; set;}
public string accBilZip{get; set;}
public string accBilCity{get; set;}
public Decimal exclPrice{get; set;}
public decimal InclPrice{get; set;}
public decimal taxAmount{get; set;}
public decimal bulkDiscount{get; set;}
public enum SORT_BY {
    POS
    }
public QuoteViewPrint(){
    QuoteID = Apexpages.currentPage().getParameters().get('id');
    system.debug('sdfsdfg'+QuoteID);
    loadData();
}

//Wrapper class to load the SNO, Product details, Quantity and Image
public class QLWrap{
    public string FT{get; set;}
    public QuoteLineItem objWrQL{get; set;}
    public integer QTY{get; set;}
    public string imageURL{get; set;}
    public Decimal DiscAmount{get; set;}        
    /* public QLWrap (string f,QuoteLineItem QT, integer q, string u, decimal d ){        
         FT= f;
        objWrQL = QT;
        QTY= q;
        imageURL = u;
        DiscAmount = d;
    } */



}

public void loadData(){
    QLI = new list<QuoteLineItem>();
    lstWrap = new list<QLWrap>();
    list<string> uniKey = new list<string>();
    Map<string,string> unikMap = new Map<string,string>();
    //Get the count of quote lines based on Name, colour, condition and condition of the product
    AggregateResult[] groupedResults = [SELECT Product2.Name prdName,Product2.Colour__c clr, Product2.Condition__c cnd,Product2.Generation__c gen, Count(Pos__c)str from QuoteLineItem 
                                            where QuoteId =: QuoteID GROUP BY Product2.Name ,Product2.Colour__c, Product2.Condition__c,Product2.Generation__c order by count(Pos__c) Desc];
    for(AggregateResult ar:groupedResults ){

        unikMap.put((string.valueOF(ar.get('prdName'))+string.valueOf(ar.get('clr'))+string.valueOf(ar.get('cnd'))+string.valueOf(ar.get('gen'))),string.valueOf(ar.get('str')));
    }

    //Get all the Quote line items to get start and end numbers
    QLI= [select Id,Product2Id,Product2.Name,Quote.QuoteNumber,Quote.Contact.Name,Quote.Discount_Amount__c,Quote.Bulk_Discount_Total__c,Quote.Grand_Total_Exclude__c,Quote.MWST_Calculation__c,
            Quote.MwSt_Procent__c,Quote.Subtotal_Exclude__c,Quote.Total_Price_Exclude__c,//Discount_Amount__c,
            Quote.Subtotal,Quote.GrandTotal,Quote.Contact.LastName,Quote.Contact.Account.billingCity,Quote.Contact.Account.billingstreet,Quote.Contact.Account.billingpostalCode,Quote.Contact.Account.Name,
            Quote.Contact.Account.Phone,Quote.Contact.MobilePhone,Quote.Contact.Email,Product2.Colour__c,Product2.Product_Image__r.skuid__Url__c,Product2.Related_Image__r.skuid__Url__c,Sales_Price_Exclude__c,
            LineNumber,Product2.Article_Image__c,Product2.Condition__c,PriceBookEntry.Name,Product2.Generation__c,UnitPrice,Discount_Amount__c,Bulk_Discount__c,Pos__c from QuoteLineItem 
            where QuoteId =:QuoteID order by Pos__c];

    string strST,strND,imgURL;
     QuoteLineItem objQLI = new QuoteLineItem();
     boolean firstime= false;
     exclPrice = 0.00;
     InclPrice = 0.00;
     bulkDiscount = 0.00;
    for(string str:unikMap.keyset()){
         imgURL = '';
         QLWrap QLWrap1 = new QLWrap();
         boolean uniString = false;
         Decimal disAmount = 0;
         system.debug('**********'+str);

         for(QuoteLineItem objQL:QLI){


            if(firstime == false){
                 accName = objQL.Quote.Contact.Account.Name;
                 accPhone = objQL.Quote.Contact.Account.Phone;
                 MobPhone = objQL.Quote.Contact.MobilePhone;
                 conEmail = objQL.Quote.Contact.Email;
                 QuoteNo =  objQL.Quote.QuoteNumber;
                 conLastName = objQL.Quote.Contact.LastName;
                 conFulName = objQL.Quote.Contact.Name;
                 accBilStrt = objQL.Quote.Contact.Account.billingstreet;
                 accBilZip = objQL.Quote.Contact.Account.billingpostalCode;
                 accBilCity = objQL.Quote.Contact.Account.billingCity;
                 firstime = True;                    
             }

            if((string.valueOF(objQL.Product2.Name)+string.valueOf(objQL.Product2.Colour__c)+string.valueOf(objQL.Product2.Condition__c)+string.valueOf(objQL.Product2.Generation__c)).equals(str)){                     
                if(uniString ==false ){
                strST = string.valueOf(objQL.Pos__c);

                if(objQL.Product2.Related_Image__r.skuid__Url__c != null)
                imgURL = string.valueOf(objQL.Product2.Related_Image__r.skuid__Url__c);
                system.debug('**********'+objQL.Pos__c);
                uniString  = true;
                objQLI = objQL;
                }
                if(objQL.Discount_Amount__c != null) 
                disAmount += objQL.Discount_Amount__c;
                strND = string.valueOf(objQL.Pos__c);
                system.debug('SE no::>:::::'+strST+'--'+strND); 
                if(objQL.Sales_Price_Exclude__c!=null)
             exclPrice = exclPrice+objQL.Sales_Price_Exclude__c;
             InclPrice = InclPrice+objQL.UnitPrice; 
             bulkDiscount = bulkDiscount+objQL.Discount_Amount__c;

            }
         }
         QLWrap1.FT = (strST+'-'+strND);
         QLWrap1.objWrQL = objQLI;
         QLWrap1.QTY = integer.valueOf(unikMap.get(str));
         QLWrap1.DiscAmount =  disAmount;            
         QLWrap1.imageURL = imgURL.removeStart('<img src="').removeEnd('" alt="[Click Here]" height="100" width="200" border="0"/>');
         system.debug('####'+imgURL.removeStart('<img src="').removeEnd('" alt="[Click Here]" height="100" width="200" border="0"/>'));
         system.debug('****'+imgURL);
         lstWrap.add(new QLWrap());

     }

     taxAmount= Decimal.valueOf( (string.valueOf(InclPrice*0.08)).removeEnd('00'));
    // exclPrice =Decimal.valueOf( (sting.valueOf(InclPrice*0.08)).removeEnd('00'));
    // lstWrap.sort();


}

 public string id;

   // lstWrap.sort();
}

Here i want to sort based on Wrapper FT string.

14

Implement the Comparable interface: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_comparable.htm

To do this you implement Comparable:

public class QLWrap implements Comparable

Then you need to implement a compareTo method:

global Integer compareTo(Object compareTo)
{
    //I don't know what criteria you want to sort on
    QLWrap other = (QLWrap) compareTo;
    if (this.aVal > other.aVal)
      return 1;
    else if (this.aVal == other.aVal)
      return 0;
    return -1;
}

After you implement that you can simply sort with a List.sort();

lstWrap.sort();

Your code sample doesn't give any information about your Wrapper class so the best I can do is give you a generic that you should be able to apply to your own code fairly easily.

EDIT: You likely need to implement the Interface in your QLWrap class.

  • I have edited the the post and added my controller code in the post. Can you pl tell me where i need to merge the comparable interface? – Reddy Nov 25 '14 at 6:56
  • @Reddy Added an update to my answer to tell you where to implement the interface. – dphil Nov 25 '14 at 14:51
3

To add to dphil's answer, for your class it sounds like you want something like this:

public class QLWrap implements Comparable {

    ...

    public Integer compareTo(Object o) {
        QLWrap that = (QLWrap) o;
        if (that.FL < this.FL) {
            return -1;
        } else if (that.FL > this.FL) {
            return 1;
        } else {
            return 0
        }
    }
}

where ... is the existing code in that class.

Then outside of the loop (where you have added to the lstWrap), sort the list in place using this call:

lstWrap.sort();

so that the sort algorithm makes use of the Comparable logic you have added.

  • I have edited the the post and added my controller code in the post. Can you pl tell me where i need to merge the comparable interface? – Reddy Nov 25 '14 at 6:57
  • @Reddy As shown in my answer. – Keith C Nov 25 '14 at 8:43

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.