Skip to content

Instantly share code, notes, and snippets.

@jfirebaugh
Created April 4, 2012 17:39
Show Gist options
  • Select an option

  • Save jfirebaugh/2304164 to your computer and use it in GitHub Desktop.

Select an option

Save jfirebaugh/2304164 to your computer and use it in GitHub Desktop.
# Numeric#nonzero? returns self if num is not zero, nil otherwise.
# This behavior is useful when chaining comparisons.
array.sort { |a, b| (a.foo <=> b.foo).nonzero? || (a.bar <=> b.bar) }
# Alternatively, consider #sort_by:
array.sort_by { |e| [e.foo, e.bar] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment