Ramkumar R
Ramkumar R

Reputation: 107

C# - How to sort ListBox containing times

I have a ListBox, it contains times. I have set the display format of the time string as shown in the screenshot.

How can I sort the times morning to evening, for example:

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

enter image description here

Upvotes: 1

Views: 724

Answers (2)

Pranav Singh
Pranav Singh

Reputation: 20121

You can do it like :

For sorting based on dates:

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

OR

For sorting on time of day:

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

Source: Sort DateTime List by Time & Sort List<DateTime> Descending

Upvotes: 1

Adarsh Shah
Adarsh Shah

Reputation: 6775

You must be using a List or any collection to bind the ListBox. Sort the List or collection by the column you are binding.

lstDates.OrderByAscending(x => x.Date)

Upvotes: 1

Related Questions