+ Start a Discussion
Steve Cox 9Steve Cox 9 

Can you sort an SObject Collecton


I hava a flow that - for any contact -  constructs an SObject collection of related records.

However, this collection is not sorted. I'm looping through this collection to construct a text field from the record names, but I would like to have this in alphabetical order. The only facility appears to be the 'ascending' or 'descending' option for the loop, but that appears to be for the index, not values within the objects.

Any suggestions?
Hi Steve,

You can refer this link for sorting in sobjects:
Steve Cox 9Steve Cox 9
Thanks for the reply, but I can see where I can introduce this code within the Wisual Workflow. Do you know if that's possible?

Steve Cox 9Steve Cox 9
Ah, I see, sorry, I hadn't added the InvokableMethod tag for it to appear in the palette.

However, I still have an issue and wonder if you can help?

In the flow, I have various sObject variables. One is called 'subscription' and is for processing a single custom Subscription object in a loop. There is also an sObject Collection variable called 'subsciptions' - which is the one I want to sort.

Here is the simple code for the Apex class:
public class sortSubs {
    @InvocableMethod(label='sort Subscriptions' description='Sorts the list of Subscriptions sObjects')
    public static void reSort(List<Subscription__c> theseSubs) {

When dropping the new apex action only the flow designer, the only possible choice for the target parameter is theseSubs - which is correct. However, wth this selected, the only options for the source is the 'subscription' sObject variable (and other single sObject variables). For some reason, my definition of theseSubs in the Apex class is coming across as a collection - only an individual sObject variable.

Can you see what I may have done wrong?


Lomash RegmiLomash Regmi
We need wrapper class method using List-of-List of sObjects to work properly in Flow.

public class SessionWrapper implements Comparable
    // This wrapper class was written to do a custom sorting operation on a list of Session__c sObjects   
    public Session__c s;

    // Constructor
    public SessionWrapper(Session__c innerS)
        s = innerS;
    // Compare sessions based on session_date__c
    public Integer compareTo(Object oFromOutside)
        // Cast argument to SessionWrapper
        SessionWrapper swFromOutside = (SessionWrapper) oFromOutside;        
        System.debug('Comparing: [' + s.Session_Date__c + '] and [' + swFromOutside.s.Session_Date__c + ']');
        // The return value of 0 indicates that both elements are equal.
        Integer returnValue = 0;
        if (s.Session_Date__c > swFromOutside.s.Session_Date__c)
            // Set return value to a positive value.
            returnValue = 1;
        else if (s.Session_Date__c < swFromOutside.s.Session_Date__c)
            // Set return value to a negative value.
            returnValue = -1;
        System.debug('Return value is [' + returnValue +']');
        return returnValue;       

    // This method sortBySessionDate is used in a flow to sort a list of sessions by Session_Date__c
    @InvocableMethod(label='Sort Sessions' description='Sorts the list of Session__c sObjects by Session_Date__c')
    public static List<List<Session__c>> sortBySessionDate(List<List<Session__c>> theSessions)
        List<List<Session__c>> lolos = new List<List<Session__c>>();
        List<SessionWrapper> sw = new List<SessionWrapper>();
        for(Session__c oneS: theSessions[0])
            sw.add(new SessionWrapper(oneS));

        // List-of-List of sObjects to return back to flow
        for(SessionWrapper oneSW: sw)
        return theSessions;