Skip to content

Instantly share code, notes, and snippets.

@yellow5
Created September 13, 2010 18:42
Show Gist options
  • Save yellow5/577787 to your computer and use it in GitHub Desktop.
Save yellow5/577787 to your computer and use it in GitHub Desktop.
ordered_ids = [5,2,3,1,4] # some array of ordered ids
records = Record.find(ordered_ids) # find the records that go with the ordered_ids
# loop the records and store them in a temp hash
temp_hash = {}
records.each do |record|
temp_hash[record.id] = record
end
# convert the temp_hash into a new array
ordered_records = ordered_ids.map{|i| temp_hash[i]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment