0

Is there any standerd methord to get all Sobjects name in Ascending order. I am able to get all objects label and API name by the following code. I need to put all the names into map in ascending order.

map<string,string> allObjects = new map<string,string>();
for(Schema.SObjectType result : Schema.getGlobalDescribe().values()){
    Schema.DescribeSObjectResult o = result.getDescribe();
    allObjects.put(o.getName(),o.getLabel());
}

2 Answers 2

2

Map not support sorting, So I achieved sorting by adding all object names in a list<sting> first and then sorting the list using sort() method.

list<string> apiName = new list<string>();
for(Schema.SObjectType o : Schema.getGlobalDescribe().values()){
    Schema.DescribeSObjectResult objResult = o.getDescribe();
    apiName.add(objResult.getName()+'@!@'+objResult.getLabel());
}
apiName.sort();
map<string,string> allObjects = new map<string,string>();
for(string s : apiName){
    list<string> objName = s.split('@!@');
    system.debug(objName);
    allObjects.put(objName[1],objName[0]);
}
1

If you use your above code then map 'allObjects' already have values in Ascending order. You don't need to change that map.

1
  • 1
    No it's not sorted Commented Jun 2, 2020 at 9:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .