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 7638573
In Process

The Archive Base Latest Questions

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

I have a large amount of data that is retrieved from a database. They

  • 0

I have a large amount of data that is retrieved from a database. They are ordered by timestamp (which each item has) and I wish to quickly scroll smoothly through this data as well as being able to retrieve the next/previous record given a specific time.

Is there any data structure that will help me do this? At the moment I do a kind of binary search through the data to retrieve the next items.

Inserts, deletions and edits are rare (but occasionally necessary) so seek time is the most critical.

Any ideas?

A simple example:

public class TimedDataItem
{
    DateTime Timestamp { get; set; }
}

// Large populated timestamped data set    
IList<TimedDataItem> timedDataItemsList = new Last<TimedDataItem>();

// Get a 'random' time
DateTime myTime = DateTime.Now;

// Find items around that 'random' time
TimedDataItem next = timedDataItemsList.FirstOrDefault(t=>t.Timestamp > myTime);
TimedDataItem previous = timedDataItemsList.LastOrDefault(t=>t.Timestamp < myTime);

// Also foreach over the collection in time order if required
foreach (TimedDataItem item in timedDataItemsList)
    DoStuff(item);

// Inserts, deletions, edits are extremely rare

Thanks.

  • 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-31T08:12:28+00:00Added an answer on May 31, 2026 at 8:12 am

    Edited answer

    Preferred solution – SortedSet

    plus: sorted collection, allows logic use of elements within a certain range

    minus: not sure what search algorithm, no previous and next

    public class TimedDataItem   
    {
        public DateTime Timestamp { get; set; }
    }
    class TimedDataItemComparer : IComparer<TimedDataItem>
    {
        public int Compare(TimedDataItem x, TimedDataItem y)
        {
            return x.Timestamp.CompareTo(y.Timestamp);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            SortedSet<TimedDataItem> ss = 
                new SortedSet<TimedDataItem>(new TimedDataItemComparer());
    
            // example data
            ss.Add(new TimedDataItem() { Timestamp = DateTime.Now.AddDays(-5) });
            TimedDataItem min = new TimedDataItem() { Timestamp = DateTime.Now.AddDays(-3) };
            ss.Add(min);
            ss.Add(new TimedDataItem() { Timestamp = DateTime.Now.AddDays(-1) });
            ss.Add(new TimedDataItem() { Timestamp = DateTime.Now });
            ss.Add(new TimedDataItem() { Timestamp = DateTime.Now.AddDays(1) });
            TimedDataItem max = new TimedDataItem() { Timestamp = DateTime.Now.AddDays(3) };
            ss.Add(max);
            ss.Add(new TimedDataItem() { Timestamp = DateTime.Now.AddDays(5) });
    
            // get elements in range
            SortedSet<TimedDataItem> view = ss.GetViewBetween(min, max);
    
            foreach (TimedDataItem item in view)
            {
                Console.WriteLine(item.Timestamp);
            }
        }    
    }
    

    Solution SortedList

    plus: sorted, typeSafe, allows next and prev

    minus: linear search

     SortedList<TimedDataItem, TimedDataItem> sl =
                new SortedList<TimedDataItem, TimedDataItem>(new TimedDataItemComparer());
    
     TimedDataItem first = new TimedDataItem() { Timestamp = DateTime.Now.AddDays(-5) };
     TimedDataItem second = new TimedDataItem() { Timestamp = DateTime.Now.AddDays(-3) };
     TimedDataItem third = new TimedDataItem() { Timestamp = DateTime.Now.AddDays(-1) };
     TimedDataItem fourth = new TimedDataItem() { Timestamp = DateTime.Now };
     TimedDataItem fifth = new TimedDataItem() { Timestamp = DateTime.Now.AddDays(1) };
     TimedDataItem sixth = new TimedDataItem() { Timestamp = DateTime.Now.AddDays(3) };
     TimedDataItem seventh = new TimedDataItem() { Timestamp = DateTime.Now.AddDays(5) };
    
     sl.Add(first, first);
     sl.Add(second, second);
     sl.Add(third, third);
     sl.Add(fourth, fourth);
     sl.Add(fifth, fifth);
     sl.Add(sixth, sixth);
     sl.Add(seventh, seventh);
    
     // unfortunatelly according to MSDN: 
     //   This method uses a linear search; therefore, this method is 
     //   an O(n) operation, where n is Count.
     int index = sl.IndexOfKey(third);
     TimedDataItem prev = sl.ElementAt(index - 1).Value;
     TimedDataItem next = sl.ElementAt(index + 1).Value;
    

    Solution ArrayList

    plus: allows use of an index, binarySearch (!)

    minus: what should you do with an index in an unordered collection …

    ArrayList al = new ArrayList();
    al.Add(first);
    al.Add(second);
    al.Add(third);
    al.Add(fourth);
    al.Add(fifth);
    al.Add(seventh);
    al.Add(seventh);
    
    int index2 = al.BinarySearch(third, new TimedDataItemComparer2());
    // al[index2] does not make sense
    // as there is no guarantee, that al[index2-1] is the element
    // with previous DateTime ...
    
    class TimedDataItemComparer2 : IComparer
    {
        public int Compare(object x, object y)
        {
            if (x is TimedDataItem && y is TimedDataItem)
                return ((TimedDataItem)x).Timestamp.
                           CompareTo(((TimedDataItem)y).Timestamp);
            else
                return -1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large amount of Data, which has to be arranged in the
I have a large amount of data that I am pulling from an xml
I have a windows service that receives a large amount of data that needs
I have a rather large amount of data (100 MB or so), that I
I have a stored proc that processes a large amount of data (about 5m
I have a large amount of data in a database. When I attempt to
I have a database which I regularly need to import large amounts of data
I have a text file with a large amount of data which is tab
So I have a Visual Studio 2008 project which has a large amount of
I have a large amount of text data that I want to display in

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.