Last active
August 29, 2015 14:05
-
-
Save edymerchk/e5d5985c8abe96ab92da to your computer and use it in GitHub Desktop.
order strings like numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can you sort them in descending order?