你如何对两个阵列进行完全相同的排序?

Chr*_*ris 1 ruby arrays sorting

你会如何以同样的方式对两个数组进行排序?

hey = %w[e c f a d b g]
hoo = [1,2,3,4,5,6,7]
hey.sort      #=> [a,b,c,d,e,f,g]
hoo.same_sort #=> [4,6,2,5,1,3,7]
Run Code Online (Sandbox Code Playgroud)

pan*_*ang 6

试试:

hey.zip(hoo).sort
=> [["a", 4], ["b", 6], ["c", 2], ["d", 5], ["e", 1], ["f", 3], ["g", 7]]

hey.zip(hoo).sort.transpose
=> [["a", "b", "c", "d", "e", "f", "g"], [4, 6, 2, 5, 1, 3, 7]]
Run Code Online (Sandbox Code Playgroud)