You need to sign in to do that
Don't have an account?

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
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
List<contact> lstcont = [SELECT Id, Name, AccountId, Sequence__c FROM contact WHERE AccountId = :accids ORDER BY Account.Name, Sequence__c];
All Answers
List<Contact> lstcont = [SELECT Id, Name, AccountId, Sequence__c FROM Contact WHERE AccountId = :accids ORDER BY Account.Name];
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]);
}
List<contact> lstcont = [SELECT Id, Name, AccountId, Sequence__c FROM contact WHERE AccountId = :accids ORDER BY Account.Name, Sequence__c];