Skip to content

Instantly share code, notes, and snippets.

@morganp
Created August 6, 2011 21:52
Show Gist options
  • Save morganp/1129788 to your computer and use it in GitHub Desktop.
Save morganp/1129788 to your computer and use it in GitHub Desktop.
A simple natural sorting
def sensible_sort(unsorted)
return unsorted.sort_by {|k| k.to_s.split(/((?:(?:^|\s)[-+])?(?:\.\d+|\d+(?:\.\d+?(?:[eE]\d+)?(?:$|(?![eE\.])))?))/ms).map{|v| Float(v) rescue v.downcase}}
end
# 1
# 2
#11
#12
##not
# 1
#11
#12
# 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment