Sort list by date

I have a WPView ListView with a column that has dates. I would like the user to sort dates (by date, not by line). Is there any way to do this? I am currently using list.Items.SortDescriptions to sort using another column, but I would like to change this to sort by date column instead.

Thanks.

+6
c # listview wpf xaml
source share
3 answers

Have you tried this?

ICollectionView view = CollectionViewSource.GetDefaultView(listView.ItemsSource); view.SortDescriptions.Add(new SortDescription("Date", ListSortDirection.Descending)); 

EDIT: you can also see this attached property

+7
source share

If you find that the default WPF sorting method is not what you planned, you can provide a custom default sorter:

 ListCollectionView view = (ListCollectionView)CollectionViewSource.GetDefaultView(listView.ItemsSource); view.CustomSort = new DateValueSorter(); listView.Items.Refresh(); 

Where DateValueSorter is a class that implements the IComparer interface and sorts according to the date value and produces the order you are looking for.

+3
source share

Use an ObservableList and a custom object type - and do the sorting yourself. Connect the ListView to an ObservableList using an ItemsSource.

... or you can try to do what this blog describes; Sort ListViews

+1
source share

All Articles