Skip to content

Instantly share code, notes, and snippets.

@websymphony
Created February 4, 2013 20:49
Show Gist options
  • Save websymphony/4709615 to your computer and use it in GitHub Desktop.
Save websymphony/4709615 to your computer and use it in GitHub Desktop.
Sorting a ruby array of objects by an attribute that could be nil.
foo=[nil, -3, 100, 4, 6, nil, 4, nil, 23]
foo.sort{|a,b|( a and b ) ? a <=> b : ( a ? -1 : 1 ) }
RESULT=> [-3, 4, 4, 6, 23, 100, nil, nil, nil]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment