How do I sort an integer array while also keeping identical elements apart from each other?

Given this array:

[38, 38, 40, 40, 40, 41, 41, 41, 41, 60]

How do I sort it into this?

[38, 40, 41, 60, 38, 40, 41, 40, 41, 41]

-------------------------------------------------------

max = arr.map { |e| arr.count(e) }.max
arr.sort. group_by { |e| e }.values. map { |a| a.fill(nil, a.size..max-1) }.transpose.flatten.compact

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章