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
ram @SFDCram @SFDC 

how to sort a list of s object names or strings in a particular order

 Hi,
I need a solution for below requirement, i tried but i am not getting proper result. Please help me on this.

1. if list returns == account, case, opportunity, contracts, idea, product, order, solution, campaign
     order should be    account, case, opportunity, remaining all are in alphabetical order.
2. if list returns == account, opportunity, contracts, idea, product, order, solution, campaign
 order should be        account, opportunity, remaining all are in alphabetical order.
3. if list returns == case, opportunity, contracts, idea, product, order, solution, campaign
 order should be       case, opportunity, remaining all are in alphabetical order.
4. if list returns == account, case, contracts, idea, product, order, solution, campaign
 order should be       account, case, remaining all are in alphabetical order.
5. etc
how we achieve this
Shubham NandwanaShubham Nandwana

Hi Ram,
You can write the query with objects in the order you want to get the desired results.

FIND {MyContactName} RETURNING Contact(Name, Id ORDER BY Name), CustomObject(Description, Id ORDER BY Description)
Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts

 

Niraj Kr SinghNiraj Kr Singh
Hi Ram,

For this kind of scenario, there is no direct solution.
We can achive this through via Custom Label/Custom setting:
Step 1: If custom label (LabelName: ObjectNamePrioritise), Keep your Prioritise value here like: Value-> account,case,opportunity .....
        String allObjectName = Label.ObjectNamePrioritise;
        
Step 2: Assuming your all list like
    List<String> allObjectName  = new List<String>{'account', 'case', 'opportunity', 'contracts', 'idea', 'product', 'order', 'solution', 'campaign'};

Step 3: Get Final OK list: finalOKlist    

Try this code:
List<String> allObjectNameList = new List<String>{'account', 'case', 'opportunity', 'contracts', 'idea', 'product', 'order', 'solution', 'campaign'};
Set<String> allObjectNameSet = new Set<String>();
allObjectNameSet.addAll(allObjectNameList);

//String allObjectName = 'account,opportunity,case'; //Uncomment this line to test it by giving any 1st order value, and comment below line.
String allObjectName = Label.ObjectNamePrioritise; // Keep above line kind of value in your custom label. In same order will get in final OK list.
List<String> fixedOrderList = new List<String>();
List<String> remainingList = new List<String>();
List<String> finalOKlist = new List<String>();
if(allObjectName != null) {
    List<String>  objectNameList= allObjectName.split(',');

    for(String fixedName : objectNameList) {
        if(allObjectNameSet.contains(fixedName)) {
            fixedOrderList.add(fixedName);
        }
    }
    //Remove filtered fixed Name from All names list
    allObjectNameSet.removeAll(fixedOrderList);
    system.debug('----allObjectNameSet----' + allObjectNameSet);
	
    //Set is sorted here for remaining names so we have to reiterate on list and take value from there.
    if(allObjectNameSet.size() > 0) {
        for(String strTest : allObjectNameList) {
            if(allObjectNameSet.contains(strTest)){
                remainingList.add(strTest);
            }
        }
        remainingList.sort();
        finalOKlist.addAll(fixedOrderList);
        finalOKlist.addAll(remainingList);
	}
    else {
        finalOKlist.addAll(fixedOrderList);
    }
}
else {
    finalOKlist.addAll(allObjectNameList);
}
system.debug('----finalOKlist----' + finalOKlist);
Mark your answer if it works
    
Thanks
Niraj