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
Jason FungJason Fung 

How to sort a generic sObject List by a field?

Does anyone has sample code to show how to sort a generic sObject List by a field?
Best Answer chosen by Jason Fung
Jason FungJason Fung
I ended up creating a bubble sort method to sort my list.   Thanks for your help anyways, Thermo.

Below is my code:

// Sort Activities by ActivityDate. This is a bubble sort algorithm.
 public void sortActivitiesByActivityDate() {
        Integer n = ActivityList.size();
        // Create a temporary variable to hold a bubble sort temp. The sObject type has to exist in your generic sObject lists. In my case, I have Task           // and Event sObjects in my list and I can use either "new Task() or new Event()".
        sObject temp = new Task();                   
        
        for (Integer i = 0; i < n; i++) {
            for (Integer j = 1; j < (n-i); j++) {   
                // Sort by Descending order by ActivityDate. To sort by ascending order, change "<" to ">" in the line below.  
                if ((Date)ActivityList[j-1].get('ActivityDate') < (Date)ActivityList[j].get('ActivityDate')) {   
                     temp = ActivityList[j-1];
                     ActivityList[j-1] = ActivityList[j];
                     ActivityList[j] = temp;
                }            
            }
        }
    }  




 

All Answers

Dilip_VDilip_V
Hi Jason,

Try this once.
 
public class ClassName{
       
        public List<SelectOption> objects {get; set;}
        public List<SObject> records {get; set;}
        public string choosenObject {get; set;}
         
        public ClassName(){
               objects = new List<SelectOption>();
               records = new List<SObject>();
               Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
                Set<String> objectSet = gd.keySet();
        for(String str:objectSet){
            //praparing label and values in selection option.
            SelectOption op =new SelectOption(str,str);
            objects.add(op);
               }

        }

        public void queryMe(){
               if(string.isNotBlank(choosenObject))
               records = database.query('select Name,Id from '+choosenObject);
               //you may need to remove Name field for some objects
        }
}

Vf page:
 
<apex:page controller="ClassName">
    <apex:form >
        <apex:selectList value="{!choosenObject}" size="1">

              <apex:selectoptions value="{!objects}"/>

        </apex:selectList>


<br/>
       <apex:commandbutton value="Query" action="{!queryMe}" rerender="output"/>

         <apex:dataTable id="output" value="{!records}" var="r">
          <apex:column value="{!r}"/>

                  </apex:dataTable>

    </apex:form>

</apex:page>


Let me know if it helps.

Mark it as best answer if it helps.

Thanks.

Jason FungJason Fung
I ended up creating a bubble sort method to sort my list.   Thanks for your help anyways, Thermo.

Below is my code:

// Sort Activities by ActivityDate. This is a bubble sort algorithm.
 public void sortActivitiesByActivityDate() {
        Integer n = ActivityList.size();
        // Create a temporary variable to hold a bubble sort temp. The sObject type has to exist in your generic sObject lists. In my case, I have Task           // and Event sObjects in my list and I can use either "new Task() or new Event()".
        sObject temp = new Task();                   
        
        for (Integer i = 0; i < n; i++) {
            for (Integer j = 1; j < (n-i); j++) {   
                // Sort by Descending order by ActivityDate. To sort by ascending order, change "<" to ">" in the line below.  
                if ((Date)ActivityList[j-1].get('ActivityDate') < (Date)ActivityList[j].get('ActivityDate')) {   
                     temp = ActivityList[j-1];
                     ActivityList[j-1] = ActivityList[j];
                     ActivityList[j] = temp;
                }            
            }
        }
    }  




 
This was selected as the best answer