Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort ListView by Date

I have a WPF ListView with a column that has dates in it. I would like a way to custom sort the dates (by date value, rather than string value). Is there a way to do this? Right now I am using list.Items.SortDescriptions to sort using another column, but I would like to change this to sort on the date column instead.

Thanks.

like image 688
Max Avatar asked Jun 03 '09 18:06

Max


3 Answers

Have you tried that ?

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

EDIT: You can also have a look at this attached property

like image 102
Thomas Levesque Avatar answered Nov 11 '22 14:11

Thomas Levesque


If you're finding that the way WPF is sorting by default isn't what you intended, you can provide a custom sorter to the default view:

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 ordering you are looking for.

like image 23
Nicholas Armstrong Avatar answered Nov 11 '22 14:11

Nicholas Armstrong


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

..., or you could try doing what this blog describes; Sorting ListViews

like image 25
Thies Avatar answered Nov 11 '22 14:11

Thies