function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
balakrishna.sfdc1.3966051675866772E12balakrishna.sfdc1.3966051675866772E12 

custiomize list sobject order by Field

Hello All,

I have one sample list to retreive contact information and data is below.
list<contact> lstcont = [select id,name,accountid,sequence__c from contact where accountid=:accids ];

sample data when I execute the list from developer console:

Contact{Name=a01i000000D6EeW, Account__c=acc1, Id=a01i000000D6EeWAAV, Sequence_Number__c=1},
Contact{Name=a01i000000D6EeX, Account__c=acc2, Id=a01i000000D6EeXAAV, Sequence_Number__c=1},
Contact:{Name=testaccount, Account__c=acc1, Id=a01i000000D6EeaAAF, Sequence_Number__c=2})

I want to modify the list order by Account wise, for example

Contact{Name=a01i000000D6EeW, Account__c=acc1, Id=a01i000000D6EeWAAV, Sequence_Number__c=1},
Contact{Name=a01i000000D6EeX, Account__c=acc1, Id=a01i000000D6EeXAAV, Sequence_Number__c=2},
Contact:{Name=testaccount, Account__c=acc2, Id=a01i000000D6EeaAAF, Sequence_Number__c=1})

Please share the ideas how to change the list order as required.

Thanks,
Krishna
Best Answer chosen by balakrishna.sfdc1.3966051675866772E12
CheyneCheyne
I'm not sure that I'm entirely clear what your end goal is, but it looks like you need to sort by Account and then by Sequence, so maybe try something like 

List<contact> lstcont = [SELECT Id, Name, AccountId, Sequence__c FROM contact WHERE AccountId = :accids ORDER BY Account.Name, Sequence__c];

All Answers

CheyneCheyne
You can use an ORDER BY clause, like this:

List<Contact> lstcont = [SELECT Id, Name, AccountId, Sequence__c FROM Contact WHERE AccountId = :accids ORDER BY Account.Name];
yogeshsharmayogeshsharma
Order By can solve your problem in this regard..
balakrishna.sfdc1.3966051675866772E12balakrishna.sfdc1.3966051675866772E12
Hello..
Thanks for the response.
I need to use sequence number field in the order by field.

List<contact> lstcont = [SELECT Id, Name, AccountId, Sequence__c FROM contact WHERE AccountId = :accids ORDER BY Sequence__c];
I tried with order by clause but not working. If I use below way am getting correct results, but can't use this approach bcz hitting governor limits.

Map<ID,list<>> mapAccCContact = new Map<ID,list<contact>>();
list<ExtractReport__c> lster = new list<ExtractReport__c>();
lster = [Select Account_ID__c,id,sequence__c from ExtractReport__c where Account_ID__c =:AccountIds];

for(ExtractReport__c configrec :lster)
            {
               mapAccCContact.put(configrec.Account_ID__c, [select id,accountid,name from contact where ACCCOUNTID =:  configrec.Account_ID__c order by b_SEQUENCE_NUMBER__c]);
}
CheyneCheyne
I'm not sure that I'm entirely clear what your end goal is, but it looks like you need to sort by Account and then by Sequence, so maybe try something like 

List<contact> lstcont = [SELECT Id, Name, AccountId, Sequence__c FROM contact WHERE AccountId = :accids ORDER BY Account.Name, Sequence__c];
This was selected as the best answer