I believe that I have found very interested undocumented bug in Salesforce.

I don't see in the documentation saying that List.sort method implementation depends on version, but looks like it does. If one reads SFDC Documentation on List sort method here and here, one may get impression that for list of SObject records of the same type with different names, the sort method will always sort them by name disregarding the version of class where the sort method is called, but it is wrong impression.

In fact, if you moved a single line of code


from a controller class with version 37 to some DAO class of version 19 you may find that list of SObject records now are sorted in some wrong order. They are no longer sorted alphabetically by the name. If you call that method again in controller class with version 37, the records will be sorted correctly. Also if you change the version of DAO Class from version 19 to version 37, the records will be also sorted as expected.

I wonder why the implementation of sort method has changed?

Does anyone know in which order Salesforce sorts SObject records if using version 19?

  • Depending on which part of the two different links you're referring to, there could be some confusion. The first link refers to sorting lists of different sObjects and also refers to custom sorts using comparable in wrapper classes which is not the same as the system method of list.sort(). The default sort order is ascending for both. It would be helpful if you provided details of the discrepancy you're seeing between the two different APIs. Right now, you've provided no examples to support your assertion of a difference nor in which type of sorting between APIs.
    – crmprogdev
    Commented Jul 28, 2016 at 15:29
  • @crmprogdev Actually I am referring to the documentation about sorting SObjects, which is namely the first link. However, looking for explanation of unexpected behavior I also read the documentation about general List.sort method in case if Salesforce might have put explanation of such strange behavior there. I am not talking about comparable interface here.
    – Patlatus
    Commented Jul 28, 2016 at 15:41
  • 1
    Lots of methods have changed since API 19. Again, please share details regarding the difference in behavior you're seeing. In particular, it would be helpful to know if the behavior doesn't match with the current documentation?
    – crmprogdev
    Commented Jul 28, 2016 at 16:02
  • @crmprogdev I have already answered this question in my post. Again, current documentation says that records should be sorted by Name field, but when I run sort method in class with version 19, they appear to be shuffled randomly, at least they are not sorted by Name in ascending order.
    – Patlatus
    Commented Jul 29, 2016 at 8:55
  • No, they're first sorted by the label of the sObject type and then the Name of the sObject record, followed by Standard Fields in alphabetical order (if Name matches), then by Custom Fields in alphabetical order. In right in the 1st set of documentation you linked to. Thus the reason I asked for examples.
    – crmprogdev
    Commented Jul 29, 2016 at 13:24


You must log in to answer this question.

Browse other questions tagged .