Skip to content

Instantly share code, notes, and snippets.

@edymerchk
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save edymerchk/e5d5985c8abe96ab92da to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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