Created
February 24, 2011 15:18
-
-
Save anewusername1/842278 to your computer and use it in GitHub Desktop.
Array sorting
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
class Array | |
def natural_sort | |
just_letters = self.select{|el| el =~ /^[a-zA-Z]+$/} | |
numbers = self - just_letters | |
just_letters.sort + numbers.sort{|x,y| x.gsub(/[a-zA-Z]+/, '').to_i <=> y.gsub(/[a-zA-Z]+/, '').to_i} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment