How do i get a list of fields in alphabetical order in SF

谁说我不能喝 提交于 2019-12-14 02:41:12

问题


I am trying to get all the fields in an object to comeup in a alphabetical order. i have tried using apex explorer to get the fields, though most of the fields come up in an order, there are few fields which do not come up in the right order.

Getting the field names in a particular order might not affect the code working, but as a part of standards used in our project, it is expected that we use them in alphabetical order.

Thanks Prady


回答1:


This recipe will work:

List<Contact> contacts = [SELECT Name, Id FROM Contact ORDER BY Id LIMIT 10]; // We'll reorder by Name later
Map<String, Contact> contactMap = new Map<String, Contact>(); // Reversed for sorting
List<String> sortThis = new List<String>();

for(Contact c : contacts)
{
   contactMap.put(c.Name, c); // For simplicity in this example, we assume the name is unique
   sortThis.add(c.Name);
}

sortThis.sort(); // Sort by Name in this case    
List<Contact> nameSortedContacts = new List<Contact>();

for(String s : sortThis)
{
   nameSortedContacts.add(contactMap.get(s));
}

I haven't tested this but it looks accurate.




回答2:


You cannot rearrange fields in the output, their query parser just uses SOQL field list to filter its own master list of fields and you get response in whatever order their master list is.

I must admit though that this is the first time I hear of such coding standard, neither do I see any benefit from it... Just my 2 cents.




回答3:


Why don't you just sort them yourself? List has a sort method, just pass in the list of string of your fields. See the method details here




回答4:


This can be done by going into the field then click 'reorder' and tick the checkbox for "Sort values alphabetically, not in the order entered. Values will be displayed alphabetically everywhere."



来源:https://stackoverflow.com/questions/9357785/how-do-i-get-a-list-of-fields-in-alphabetical-order-in-sf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!