Skip to content

Instantly share code, notes, and snippets.

@dmichael
Created May 27, 2009 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmichael/118757 to your computer and use it in GitHub Desktop.
Save dmichael/118757 to your computer and use it in GitHub Desktop.
# Order one Array by the order of another Array in Ruby.
expected = [:john, :moishe, :bartholemew]
received = [:moishe, :bartholemew, :john]
sorted = received.sort_by {|name| expected.index(name)}
# => [:john, :moishe, :bartholemew]
# It should even handle shorter lists.
sorted = [:bartholemew, :john].sort_by{|name| expected.index(name)}
# => [:john, :bartholemew]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment