Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def reorder(things, ids)
  ordered_things = Array.new(things.size)

  unless things.size == ids.size
    (ids - (things.collect{|x| x.id})).collect{|missing| ids[ids.index(missing)] = nil}
    ids = ids.compact
  end

  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

  things.each do |thing|
    position = ids.index(thing.id)
    ordered_things[position] = thing
  end
  ordered_things
end