我有這樣的:紅寶石sort_by with_index模
input = ["a","b","c","d","a","b","c","d","a","b","c","d"]
我想這一點:
result = ["a","a","a","b","b","b","c","c","c","d","d","d"]
我嘗試這樣做:
input.sort_by.with_index do |piece, index|
index%3
end
我得到這個:
["a", "d", "c", "b", "c", "b", "d", "a", "d", "b", "a", "c"]
爲什麼?
它應該是'索引%4' –
另一種方法是'input.each_slice(4).to_a.transpose.flatten'。 –
你可以通過'result = index.sort'從'input'得到'result'嗎?不知道我明白這個問題... –