Guest User

Untitled

a guest
Jun 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. def reorder(things, ids)
  2. ordered_things = Array.new(things.size)
  3.  
  4. unless things.size == ids.size
  5. (ids - (things.collect{|x| x.id})).collect{|missing| ids[ids.index(missing)] = nil}
  6. ids = ids.compact
  7. end
  8.  
  9. raise "Out of sync! Found #{ids.size} items in index, but only #{things.size} were found in database! Remove #{(ids - (things.collect{|x| x.id})).to_sentence}." unless things.size == ids.size
  10.  
  11. things.each do |thing|
  12. position = ids.index(thing.id)
  13. ordered_things[position] = thing
  14. end
  15. ordered_things
  16. end
Add Comment
Please, Sign In to add comment