You need to sign in to do that
Don't have an account?
Don Schueler 18
Sort sObject List with multiple sObject types on CreatedDate
I am doing this...all is well except that I need to sort on CreatedDate. Is there a simple approach?
List<sObject> objects = new List<sObject>();
objects.addAll((List<sObject>)(accountList));
objects.addAll((List<sObject>)(contactList));
List<sObject> objects = new List<sObject>();
objects.addAll((List<sObject>)(accountList));
objects.addAll((List<sObject>)(contactList));
To implement a custom sort order for sObjects in lists, create a wrapper class for the sObject and implement the Comparable interface. The wrapper class contains the sObject in question and implements the compareTo method, in which you specify the sort logic.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_list_sorting_sobject.htm
You would need to convert each generic SObject back to its true type, in order to access its CreatedDate. Sadly only the ID field is accessible via the generic SObject record.
Below is some code you can tinker with.