Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:12:26+00:00 2026-05-31T11:12:26+00:00

I have two streams of objects that each have a Timestamp value. Both streams

  • 0

I have two streams of objects that each have a Timestamp value. Both streams are in order, so for example the timestamps might be Ta = 1,3,6,6,7 in one stream and Tb = 1,2,5,5,6,8 in the other. Objects in both streams are of the same type.

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 A1, then B1, B2, A3 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 [A3] on the bus, followed by [A15,A25] and so on.

I’ve tried to implement this by making two ConcurrentQueue 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.

However, I’ve encountered two problems:

  • 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).
  • I sometimes end up with a situation where I handle the event, say, A15 before A25 has arrived. I somehow need to guard against this.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:12:27+00:00Added an answer on May 31, 2026 at 11:12 am

    Rx is indeed a good fit for this problem IMO.

    IObservables can’t ‘OrderBy’ for obvious reasons (you would have to observe the entire stream first to guarantee the correct output order), so my answer below makes the assumption (that you stated) that your 2 source event streams are in order.

    It was an interesting problem in the end. The standard Rx operators are missing a GroupByUntilChanged that would have solved this easily, as long as it called OnComplete on the previous group observable when the first element of the next group was observed. However looking at the implementation of DistinctUntilChanged it doesn’t follow this pattern and only calls OnComplete when the source observable completes (even though it knows there will be no more elements after the first non-distinct element… weird???). Anyway, for those reasons, I decided against a GroupByUntilChanged method (to not break Rx conventions) and went instead for a ToEnumerableUntilChanged.

    Disclaimer: This is my first Rx extension so would appreciate feedback on my choices made. Also, one main concern of mine is the anonymous observable holding the distinctElements list.

    Firstly, your application code is quite simple:

        public class Event
        {
            public DateTime Timestamp { get; set; }
        }
    
        private IObservable<Event> eventStream1;
        private IObservable<Event> eventStream2; 
    
        public IObservable<IEnumerable<Event>> CombineAndGroup()
        {
            return eventStream1.CombineLatest(eventStream2, (e1, e2) => e1.Timestamp < e2.Timestamp ? e1 : e2)
                .ToEnumerableUntilChanged(e => e.Timestamp);
        }
    

    Now for the ToEnumerableUntilChanged implementation (wall of code warning):

        public static IObservable<IEnumerable<TSource>> ToEnumerableUntilChanged<TSource,TKey>(this IObservable<TSource> source, Func<TSource,TKey> keySelector)
        {
            // TODO: Follow Rx conventions and create a superset overload that takes the IComparer as a parameter
            var comparer = EqualityComparer<TKey>.Default;
    
            return Observable.Create<IEnumerable<TSource>>(observer =>
            {
                var currentKey = default(TKey);
                var hasCurrentKey = false;
                var distinctElements = new List<TSource>();
    
                return source.Subscribe((value =>
                {
                    TKey elementKey;
                    try
                    {
                        elementKey = keySelector(value);
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                        return;
                    }
    
                    if (!hasCurrentKey)
                    {
                        hasCurrentKey = true;
                        currentKey = elementKey;
                        distinctElements.Add(value);
                        return;
                    }
    
                    bool keysMatch;
                    try
                    {
                        keysMatch = comparer.Equals(currentKey, elementKey);
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                        return;
                    }
    
                    if (keysMatch)
                    {
                        distinctElements.Add(value);
                        return;
                    }
    
                    observer.OnNext( distinctElements);
    
                    distinctElements.Clear();
                    distinctElements.Add(value);
                    currentKey = elementKey;
    
                }), observer.OnError, () =>
                {
                    if (distinctElements.Count > 0)
                        observer.OnNext(distinctElements);
    
                    observer.OnCompleted();
                });
            });
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two raw sound streams that I need to add together. For the
I have two applications written in Java that communicate with each other using XML
Suppose I have a stream of [acme] objects that I want to expose via
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two classes, and want to include a static instance of one class
I have two threads, one needs to poll a bunch of separate static resources
I have two spreadsheets... when one gets modified in a certain way I want
I want to have two side-by-side areas where I will stream images to, passed
I have two systems I'd like to integrate, one which uses a completely in-house

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.