问题描述
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}]
另外,基于特定的条件,我将' user_id
'key from同一个散列并对它进行排序,并在下面给出相同的数组:
sorted_user_ids = [880,881,882,883,884,475,252]
现在,我需要 user_quizzes
将根据 sorted_user_ids $ c $中的
user_id
顺序进行重新排列c>数组。
任何人都可以帮助我解决这个问题。 :)
使用或,您可以指定将用于比较的密钥:
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}
]
sorted_user_ids = [880,881,882,883,884,475,252]
user_quizzes.sort_by {| x | sorted_user_ids.index(x [:user_id])}
#=> [{: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},
#{:id => 3897,: quiz_id => 1793,:user_id => 475},
#{:id => 3897,:quiz_id => 1793,:user_id => 252}]
附注: sorted_user_ids.index(x [:user_id])
可能会成为瓶颈(重复O(n)操作),如果数组很大。
构建一个映射 user_id
s在这种情况下的订单:
sorted_user_ids = [880,881,882,883,884,475,252]
order = Hash [sorted_user_ids.each_with_index.to_a]
#=> {880 => 0,881 => 1,882 => 2,883 => 3,884 => 4,475 => 5,252 => 6}
user_quizzes.sort_by {| x | order [x [:user_id]]}
#=>同上。
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
or Array#sort_by!
, you can specify the key that will be used for comparison:
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}
]
sorted_user_ids = [880, 881, 882, 883, 884, 475, 252]
user_quizzes.sort_by { |x| sorted_user_ids.index(x[:user_id]) }
# => [{: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},
# {:id=>3897, :quiz_id=>1793, :user_id=>475},
# {:id=>3897, :quiz_id=>1793, :user_id=>252}]
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:
sorted_user_ids = [880, 881, 882, 883, 884, 475, 252]
order = Hash[sorted_user_ids.each_with_index.to_a]
# => {880=>0, 881=>1, 882=>2, 883=>3, 884=>4, 475=>5, 252=>6}
user_quizzes.sort_by { |x| order[x[:user_id]] }
# => same as above.
这篇关于基于排序值数组排序哈希数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!