Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Created May 21, 2012 17:54
Show Gist options
  • Save amitpatelx/2763585 to your computer and use it in GitHub Desktop.
Save amitpatelx/2763585 to your computer and use it in GitHub Desktop.
Sort 2D array
1.9.3-p125 :007 > x=[["2010-01-10", 2], ["2010-01-09", 5], ["2009-12-11", 3], ["2009-12-12", 12], ["2009-12-13", 0]]
=> [["2010-01-10", 2], ["2010-01-09", 5], ["2009-12-11", 3], ["2009-12-12", 12], ["2009-12-13", 0]]
1.9.3-p125 :008 > x.sort_by{|k|k[1]}
=> [["2009-12-13", 0], ["2010-01-10", 2], ["2009-12-11", 3], ["2010-01-09", 5], ["2009-12-12", 12]]
1.9.3-p125 :009 > x.sort_by{|k|k[0]}
=> [["2009-12-11", 3], ["2009-12-12", 12], ["2009-12-13", 0], ["2010-01-09", 5], ["2010-01-10", 2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment