Not sure if I understand your requirements, but here's my attempt:
- Calculate each element's relative position, i.e. 1st element = position 0, middle element = position 0.5, last element = position 1
- Merge all element / position pairs in a single array
- Sort the array by the calculated position
- Extract the elements / remove the position
Example implementation:
array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
array2 = ['a', 'b', 'c']
array3 = ['x', 'y']
def pos(array)
array.map.with_index { |e, i| [e, i / (array.size - 1.0)] }
end
result = pos(array1) + pos(array2) + pos(array3)
#=> [[0, 0.0], [1, 0.1111111111111111], [2, 0.2222222222222222], [3, 0.3333333333333333], [4, 0.4444444444444444], [5, 0.5555555555555556], [6, 0.6666666666666666], [7, 0.7777777777777778], [8, 0.8888888888888888], [9, 1.0], ["a", 0.0], ["b", 0.5], ["c", 1.0], ["x", 0.0], ["y", 1.0]]
result.sort_by(&:last).map(&:first)
#=> [0, "a", "x", 1, 2, 3, 4, "b", 5, 6, 7, 8, 9, "c", "y"]
You can change the order by tweaking the calculation, e.g. (i + 1) / (array.size + 1.0)
gives:
#=> [0, 1, "a", 2, "x", 3, 4, "b", 5, 6, "y", 7, "c", 8, 9]