Skip to content

Instantly share code, notes, and snippets.

@edymerchk
Last active August 29, 2015 14:05
Show Gist options
  • Save edymerchk/e5d5985c8abe96ab92da to your computer and use it in GitHub Desktop.
Save edymerchk/e5d5985c8abe96ab92da to your computer and use it in GitHub Desktop.
order strings like numbers
# Say you have a string field in the DB that needs to be order like numbers
#If you do the following... maybe its not what you want:
Document.order(:number).pluck(:number)
=> [ "1", "10591059616", "100", "2", "22"....]
# But if you do...:
Document.order("LENGTH(documents.number), number").pluck(:number)
=> ["1", "2", "22", "99", "100", "10591059616"...]
#Maybe thats what you want :)
@lironbeni
Copy link

How can you sort them in descending order?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment