You need to sign in to do that
Don't have an account?
Jason 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?
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
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
Try this once.
Vf page:
Let me know if it helps.
Mark it as best answer if it helps.
Thanks.
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;
}
}
}
}