I use str to dynamically generate input fields on a visualforce page. Unlike standard page layouts that come with a drag and drop layout editor, my page's positioning of input fields depends on the order of str (page code not relevant here).

I populate and sort List<String> str using the following code:

SObjectType sObjectType = Schema.getGlobalDescribe().get('Opportunity');
Map<String,Schema.SObjectField> fields = sObjectType.getDescribe().fields.getMap();
List<String> str = new List<String>();

for(Schema.SObjectField sobjF : fields.values()) 
            Schema.DescribeFieldResult fieldType = sobjF.getDescribe();
            if( !fieldType.isNillable() && fieldType.isAccessible()  )    
        return str; 

How do I replace my sort() method with a custom sort method, perhaps using Apex Comparable interface, such that str is sorted by multiple fieldType properties eg sorted by fieldType.getType() ASC and fieldType.getLabel() ASC, rather than sorting by fieldType.getName()? Note: I require fieldType.getName() values to comprise the str, but i am looking for ways to sort str by multiple Schema.DescribeFieldResult properties.

It would be great if someone could demonstrate how to use the Apex Comparable Interface so that the List str are sorted not by a basic str.sort() but instead by multiple fieldType properties fieldType.getType() ASC and also by fieldType.getLabel() ASC. The answer that I’m working on would return 1 string “str” containing fieldType.getName() string values but those values would be sorted first by field type and also by field label.

UPDATE: For a psuedo example, suppose that str.sort() return str returns these fieldType.getName() values:


The psuedo example {'a','d','e','f'} reflects alphabetical ASC order (str.sort()) which is no good. Instead I want to the same list elements sorted by datatype (fieldType.getType()) alpha ascending first, then label alpha ascending (fieldType.getLabel()), in other words i am looking for a way to sort str so that its ordering reflects {'d','f','e','a'} if the getType() and getLabel() (pseudo) schema for those fieldTypes were:

'd' => checkbox, 'dogLabel'
'f' => checkbox, 'foxLabel'
'e'=> currency, 'earthwormLabel'
'a' => date, 'appleLabel'

Thanks everyone,

  • Did you look at apex comparable and try to this comparison? – RedDevil Jun 7 '18 at 10:32
  • I am seeking an answer that demonstrates using apex comparable, specifically to sort my “str” list by both FieldType.getType() ASC , FieldType.getLabel() ASC – Peter Noges Jun 7 '18 at 12:58
  • 2
    I can write something up with comparable using a wrapper class with these 2 strings is that what you are looking for? – RedDevil Jun 7 '18 at 13:09
  • Have you made any attempt to solve this problem yourself? Where specifically are you stuck? Right now it seems you are just asking the community to do your work for you. – Adrian Larson Jun 7 '18 at 13:27
  • 2
    I wrote up an example of how this should work just for my own edification. But simply handing you the implementation sort of goes against the spirit of the community. It's not a pro bono consulting site. FYI if your question is simply how to sort on two keys in one implementation, there are already answers on this site addressing that issue. – Adrian Larson Jun 7 '18 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.