This question already has an answer here:

I'm trying to 'ORDER BY' a String field which contains numbers. It orders like this:

1 10 2 3 ...

I've tried whith LPAD, VALUE, *1, but nothing works.

THANKS all for ur help!

marked as duplicate by Daniel Ballinger, Chris Duncombe, Vamsi Krishna, Eric, Jenny B Aug 25 '15 at 12:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • CAST doesnt work btw – Carloss Aug 25 '15 at 8:01

Two possible options I can think of right now:


  • Populate a map based on your query results, use this field as Integer key (use Integer.valueOf)
  • Convert the keySet into a list and apply sort
  • Iterate the list and fetch the map for results in sequence


  • Add a formula field with return type as number and use it in ORDER BY clause.

Not the answer you're looking for? Browse other questions tagged or ask your own question.