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
Rahul_2011Rahul_2011 

Sorted sObject List using ORDER BY SOQL

All,

 

I have a requirement to get a sorted list of sObject ApexCode. For each sObject set the Status Field to a pre-defined value and update the List. To acheive this I have sorted query on the sObject which returns a List. For some reason the iteration over the sObject List using the for loop does not return the results in sorted order. Execution of query itself in Eclipse returns the sorted results.

 

Here is the psuedo-code (Apex Code). Can someone tell me what is missing here:

//Check 1::

List<sObject> resultList = [Select Id, Name from sObject where value >100 order by startDate asc];

 

for (sObject result: resultList)

{

  // Check 2::

   System.debug('result start Date='+result.startDate);  

}

 

Here sObject is the custom object,

          Sort Query returns the results in the required order

          Iteration over the List is NOT ordered.

 

 

Any thoughts what am I missing here?

 

TIA

Rahul

  

raseshtcsraseshtcs
Could you try keeping the query inside the for loop and then try..
Rahul_2011Rahul_2011

Did Earlier try that as well - did not work.

lrw757lrw757

If for some reason the iteration isn't ordered, you can always use:

 

for(i=0,i<resultList.size(),i++){

sObject thisSObject = resultList.get(i);

//DO YOUR LOGIC HERE

}