title: How can I combine two streams ordered then grouped by timestamp?
body: <p>I have two streams of objects that each have a <code>Timestamp</code> value. Both streams are in order, so for example the timestamps might be T<sub>a</sub> = <code>1,3,6,6,7</code> in one stream and T<sub>b</sub> = <code>1,2,5,5,6,8</code> in the other. Objects in both streams are of the same type.</p>
  
  <p>What I'd like to be able to do is to put each of these events on the bus in order of timestamp, i.e., put A<sub>1</sub>, then B<sub>1</sub>, B<sub>2</sub>, A<sub>3</sub> and so on. Furthermore, since some streams have several (sequential) elements with the same timestamp, I want those elements grouped so that each new event is an array. So we would put [A<sub>3</sub>] on the bus, followed by [A<sup>1</sup><sub>5</sub>,A<sup>2</sup><sub>5</sub>] and so on.</p>
  
  <p>I've tried to implement this by making two <code>ConcurrentQueue</code> structures, putting each event at the back of the queue, then looking at each front of the queue, choosing first the earlier event and then traversing the queue such that all events with this timestamp are present.</p>
  
  <p>However, I've encountered two problems:</p>
  
  <ul>
  <li>If I leave these queues unbounded, I quickly run out of memory as the read op is a lot faster than the handlers receiving the events. (I've got a few gigabytes of data).</li>
  <li>I sometimes end up with a situation where I handle the event, say, A<sup>1</sup><sub>5</sub> before A<sup>2</sup><sub>5</sub> has arrived. I somehow need to guard against this.</li>
  </ul>
  
  <p>I'm thinking that Rx can help in this regard but I don't see an obvious combinator(s) to make this possible. Thus, any advice is much appreciated.</p>