0

I am trying to sort values in desc order before binding these to picklist. I have referred to some posts regarding sorting(This post) and implemented the below:

  1. Take keySet() of the map.
  2. Convert the set from Step 1. to List and sort it.
  3. Iterate through sorted list and for each element display map.get(element)

       // Get data from custom object using SOQL Query

        List<MyObj__c> ObjData = [Select Id, Rel__C from MyObj__c where Name__c=:searchByName];
        if(ObjData<>null && ObjData.size()>0)
        {
                 for(MyObj__c relObj:ObjData)
                 {
                        mapOfRel.put(relObj.Id,relObj.Rel__C);

                 }
        }
        //Below is the list to bind values to picklist.
        List<SelectOption> lstValues = new List<SelectOption>();
        if(mapOfRel<>null && mapOfRel.keySet()<>null){
                    lstValues.sort();
                    lstValues.add(new SelectOption('-None-','-None-')); 

                    for(String getIdValue:mapOfRel.keySet()){
                        lstValues.add(new SelectOption(getIdValue,mapOfRel.get(getIdValue)));    
                    }
                   // lstValues.sort();
            }
        system.debug('%%%%%%'+lstValues);
        return lstValues;

lstValues is still not sorted. Can anyone suggest where I am going wrong?

2 Answers 2

2

The post you linked isn't applicable to your situation.

Just sort the original query, and you're done.

SelectOption[] options = new SelectOption[] { new SelectOption('','-- none --') };
for(Obj__c record: [SELECT ... FROM Obj__c WHERE ... ORDER BY ... DESC]) {
  options.add(new SelectOption(record.Id, record.SomeLabel));
}
0

sort method used within the for loop.it is properly worked. // Get data from a custom object using SOQL Query

    List<MyObj__c> ObjData = [Select Id, Rel__C from MyObj__c where Name__c=:searchByName];
    if(ObjData<>null && ObjData.size()>0)
    {
             for(MyObj__c relObj:ObjData)
             {
                    mapOfRel.put(relObj.Id,relObj.Rel__C);

             }
    }
    //Below is the list to bind values to picklist.
    List<SelectOption> lstValues = new List<SelectOption>();
    if(mapOfRel<>null && mapOfRel.keySet()<>null){
                lstValues.sort();
                lstValues.add(new SelectOption('-None-','-None-')); 

                for(String getIdValue:mapOfRel.keySet()){
           lstValues.sort();
                    lstValues.add(new SelectOption(getIdValue,mapOfRel.get(getIdValue)));    
                }

        }
    system.debug('%%%%%%'+lstValues);
    return lstValues;

Please mark it as Best if it helps you.

Thanks Ajay

1
  • The values which I am trying to sort contain decimals in this format and are not text format but the field in the object is of text type : 1,2,3.2,3.1,14.2
    – saisri1
    Jun 12, 2018 at 5:40

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.