From: masta Blasta Date: 2013-06-18T00:43:11+09:00 Subject: Compare and sort one array according to another. I have two arrays of objects that look something like this: arr1 = [ Obj1(prop1, prop2, id), Obj2(prop1, prop2, id), Obj3(prop1, prop2, id) ] arr2 = [ OthrObj1(prop1, prop2, ref_id), OthrObj2(prop1, prop2, ref_id), OthrObj3(prop1, prop2, ref_id) ] The objects in arr1 are ordered by id (just as an example). The objects in arr2 have a 1-1 relationship with the objects in arr1, however they are not sorted. They need to be sorted to match the order of the objects in arr1. The order of arr1 is not predictable so arr2 has to be compared to arr1 to find the order. The two arrays are always the same size. No items should repeat, however this might be something i need to guard against. For now let's assume no items repeat. My current solution is to make a temp array and then build it with the items in order. It's ok but i'm wondering if there's a better ruby method. temp_arr = [] arr1.each do |o| index = arr2.find_index {|ele| ele.ref_id == o.id} temp_arr.push arr2[index] if index end -- Posted via http://www.ruby-forum.com/.