I have a list of objects. Each object has a DateTime
property.
When I'm adding objects, to this list the DateTime is instantiated through ParseExact.
public class Event
{
public Event(string time)
{
Time = DateTime.ParseExact(time, "HH:mm zzz", CultureInfo.InvariantCulture);
}
public DateTime Time { get; set; }
}
This allows me to add items like so:
Events.AddRange(new List<Event> { new Event("07:00 +" + timeZone), new Event("11:30 +" + timeZone), new Event("16:00 +" +timeZone), new Event("19:00 +" + timeZone), new Event("00:00 +" + timeZone), new Event("03:00 +" +timeZone) });
I'm using DateTime, because it handles the timezones in the background.
Problem is; I want to create a list that sorts the above list, with the current time.
so if the list had ["7:30", "5:12", "15:55", "22:22", "23:59"]
and the current time is 22:19, it would sort it from that time.
So it would be: ["22:22", "23:59", "5:12", "7:30", "15:55"]
I already have a function that does that:
Events.Sort((x,y) => x.Time.TimeOfDay.CompareTo(y.Time.TimeOfDay));
Problem is that, it's all the same day (Today).
To illustrate with the list above (given the date and time is 25/7 22:19, it would be:
["22:22 25/7", "23:59 25/7", "5:12 25/7", "7:30 25/7", "15:55 25/7"]
I hope this illustrates my point, as the date should turn to the 26th, past midnight, and so on...
Another thing is, that it returns a list as long as the input length, but ideally this could go on forever (hope this makes sense). It would be nice to be able to ask for 10 items, and it will return the next 10 items, from the current time.
Edit: Clarification I'm getting a list of DateTimes, only the hour and minute of the DateTime object is relevant. From this moment, I want to get the next 10, 25 or 50 DateTimes objects. This can be generated as the hours and minutes are the only relevant data in the datetime object. The items should then be ordered by now and so forth.
Edit 2: Timezones In above code, timezone = "0000", UTC. This will on the users end automatically convert the time to the users current location.
Time = DateTime.ParseExact(...);
?DateTime
values starting from now, with times of day from the list? What do you mean by "time zone" here? I strongly suspect it may not be the same as mine. If you could make it clearer what you're really trying to achieve, I suspect there could be a much better way of achieving it. I don't think this is really about sorting at all.DateTime
values at all. Within .NET types,TimeSpan
is often used for time-of-day values - personally I don't like that, butDateTime
isn't ideal either. (I'd use my Noda Time library andLocalTime
, but that's a different matter.) Will think about the simplest way of achieving this...