Skip to main content
Does anyone has sample code to show how to sort a generic sObject List by a field?
2 件の回答
  1. 2016年9月30日 0:35
    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;

                    }            

                }

            }

        }  

     
0/9000