C# - 如何对包含时间的ListBox进行排序

时间:2014-01-10 10:42:58

标签: c# arrays sorting

我有ListBox,它包含时间。我已经设置了时间字符串的显示格式,如屏幕截图所示。

我如何从早到晚排序,例如:

10:20:23 AM
11:56:65 AM
01:12:68 PM

enter image description here

2 个答案:

答案 0 :(得分:1)

您必须使用List或任何集合来绑定ListBox。按您要绑定的列对列表或集合进行排序。

lstDates.OrderByAscending(x => x.Date)

答案 1 :(得分:1)

你可以这样做:

根据日期排序:

datesList.Sort((x, y) => x.StoredDate.CompareTo(y.StoredDate));

OR

按时间排序:

var list = dateList.OrderBy(x => x.TimeOfDay).ToList(); 

来源:Sort DateTime List by Time  &安培; Sort List<DateTime> Descending