我有这样的:红宝石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'吗?不知道我明白这个问题... –