How to sort records runtime based on time slot?

  • Thread starter Thread starter ashuthinks32
  • Start date Start date
A

ashuthinks32

Guest
I have below code which is working fine for me. For demo purpose i'm adding only few records into list in real time this list is continously added with new values per second. question is how to sort that realtime data based on time slot?

public class MyObject
{
public double Value { get; set; }
public DateTime Time { get; set; }

public DateTime GetStartOfPeriodByMins(int numMinutes)
{
int oldMinutes = Time.Minute;
int newMinutes = (oldMinutes / numMinutes) * numMinutes;

DateTime startOfPeriod = new DateTime(Time.Year, Time.Month, Time.Day, Time.Hour, newMinutes, 0);

return startOfPeriod;
}
}

List<MyObject> inputList = new List<MyObject>();

inputList.Add(new MyObject() { Time = new DateTime(2020, 10, 10, 9, 15, 0), Value = 12 });
Thread.Sleep(1000);
inputList.Add(new MyObject() { Time = new DateTime(2020, 10, 10, 9, 15, 2), Value = 11 });
Thread.Sleep(1000);
inputList.Add(new MyObject() { Time = new DateTime(2020, 10, 10, 9, 15, 5), Value = 13 });
Thread.Sleep(1000);
inputList.Add(new MyObject() { Time = new DateTime(2020, 10, 10, 9, 15, 15), Value = 14 });
Thread.Sleep(1000);
inputList.Add(new MyObject() { Time = new DateTime(2020, 10, 10, 9, 15, 45), Value = 12 });
Thread.Sleep(1000);
inputList.Add(new MyObject() { Time = new DateTime(2020, 10, 10, 9, 15, 55), Value = 11 });
Thread.Sleep(1000);
inputList.Add(new MyObject() { Time = new DateTime(2020, 10, 10, 9, 16, 0), Value = 13 });
Thread.Sleep(1000);
inputList.Add(new MyObject() { Time = new DateTime(2020, 10, 10, 9, 16, 1), Value = 16 });


var resultSet = inputList
.GroupBy(i => i.GetStartOfPeriodByMins(1))
.Select(gr =>
new
{
StartOfPeriod = gr.Key,
Min = gr.Min(item => item.Value),
Max = gr.Max(item => item.Value),
Open = gr.OrderBy(item => item.Time).First().Value,
Close = gr.OrderBy(item => item.Time).Last().Value
});

var my = resultSet.ToList();


time slot taken here is 1 minutes so for every minute it will give me seperate values of that minutes open high low and close.

ue29c.png

issue is this is static data but in real time inputList will get continuous new data from API so how to sort it based on 1 minute?

i want to process data from inputList so that after every 1 minutes i will get latest dataset of recent 1 minute so that i can use it for further calculation.


SE

Continue reading...
 
Back
Top