+ Start a Discussion
yeshabyeshab 

Order by clause ignored in the apex class code

I am doing a simple query on custom object like below:

SELECT Widget__c.Name,  Id, orderid__c, (SELECT Model__c.Name FROM Widget__c.Models__r) FROM Widget__c order by orderid__c

orderid__c is a numeric field, used to sort.
it works fine in the developer console query editor, but doesn't appear in ordered form in the code.

any help would be great!!!


Thanks,


Best Answer chosen by yeshab
James LoghryJames Loghry
ty1980, a couple of things:

1) Do you have null OrderId__c values?  If so, try appending "NULLS FIRST" or "NULLS LAST" to your ORDER BY statement, depending on your requirement.
2)  Are you feeding the query values into a Map at some point in your Apex class?  Maps are unordered, so if you use maps to pass your data around, then the data will become out of order.  To fix this you'll need to use lists instead of maps.

All Answers

Pramod_SFDCPramod_SFDC
Hi,

Run this in the Workbench and cjeck it once. However, i am not sure how your are confirming that the records are not in order.


Regards
Pramod
yeshabyeshab
The query returns correct result as per the order by orderid__c  in the query editor.  The same line of sql mentioned in the apex class code is not displaying as per the orderid__c.

i am displaying the result in the visualforce page.
James LoghryJames Loghry
ty1980, a couple of things:

1) Do you have null OrderId__c values?  If so, try appending "NULLS FIRST" or "NULLS LAST" to your ORDER BY statement, depending on your requirement.
2)  Are you feeding the query values into a Map at some point in your Apex class?  Maps are unordered, so if you use maps to pass your data around, then the data will become out of order.  To fix this you'll need to use lists instead of maps.
This was selected as the best answer
yeshabyeshab
Thanks James, my issue got resolved. I was feeding the query values into a Map in the class. I am now using list  and it works fine.