It's All Writing.

Writing makes you happy.

How to get Salesforce sObject's fields List that is sorted by field's name at Apex

Environment

  • Apex

Code

String sobjectApiName = 'Account';
Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(sobjectApiName).getDescribe().fields.getMap();
List<String> fieldNames = new List<String>(fieldMap.keySet());
fieldNames.sort();
for (String fieldName: fieldNames) {
    // anything you want to do
}