I have an array of hashes as given below:
user_quizzes = [{:id => 3897, :quiz_id => 1793, :user_id => 252}, {:id => 3897, :quiz_id => 1793, :user_id => 475}, {:id => 3897, :quiz_id => 1793, :user_id => 880}, {:id => 3897, :quiz_id => 1793, :user_id => 881}, {:id => 3897, :quiz_id => 1793, :user_id => 882}, {:id => 3897, :quiz_id => 1793, :user_id => 883}, {:id => 3897, :quiz_id => 1793, :user_id => 884}]
Also, based on a particular condition I took the values of 'user_id
' key from the same hash and sorted it and the same array is given below:
sorted_user_ids = [880, 881, 882, 883, 884, 475, 252]
Now, I need the user_quizzes
to be rearranged based on the order of user_id
in sorted_user_ids
array.
Can anyone please help me on this. :)
Using
Enumerable#sort_by
orArray#sort_by!
, you can specify the key that will be used for comparison:Side note:
sorted_user_ids.index(x[:user_id])
can become bottleneck (repeat O(n) operations), if the array is huge.Build a hash that maps
user_id
s to orders in such case: