0

So I am having a list with objects. Those objects have a property TimeStamp. The problem is, this property is a string. Now when sorting the list by TimeStamp, the sort function ignores "AM" and "PM"

var historicalAlarms = new List<IHistoricalAlarmItem>();
foreach(...)
{ 
    ... 
}

historicalAlarms.Sort((x, y) => ((Belimed.Alarm.HistoricalAlarmItem)x).TimeStamp.CompareTo(((Belimed.Alarm.HistoricalAlarmItem)y).TimeStamp));

Is it possible to convert the TimeStamp to a new DateTime object to make the Sort function not ignore AM and PM? Remember that the objects in the List also need to be cast to another type (unfortunately they have the same name)

This works:

historicalAlarms.Sort((x, y) => ((Belimed.Alarm.HistoricalAlarmItem)x).ActivationTime.CompareTo(((Belimed.Alarm.HistoricalAlarmItem)y).ActivationTime));

This doesnt:

historicalAlarms.OrderBy(x => DateTime.Parse((Belimed.Alarm.HistoricalAlarmItem)x.TimeStamp))
user2877820
  • 287
  • 4
  • 19

1 Answers1

2

You can use OrderBy and DateTime.Parse to achieve that.

historicalAlarms = historicalAlarms
    .OrderBy(value => DateTime.Parse(value.TimeStamp))
    .ToList();
NtFreX
  • 10,379
  • 2
  • 43
  • 63