Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a complex custom sort in ListView

I can't find how to add custom sorting to WPF ListView control? I don't need to sort by columns asc or desc, I need a custom one with some algorithm in it?

can you help me?

Thank you!

like image 445
Artem Makarov Avatar asked Jan 28 '26 19:01

Artem Makarov


1 Answers

You can use CustomSort property on ListCollectionView provided that you implement IComparer for your class:

//somewhere in code behind of your window:
var view = (ListCollectionView)CollectionViewSource.GetDefaultView(Items); //Items is an ObservableCollection<T> 
view.CustomSort = new MyComparer(); //MyComparable implements IComparer

Any control bound to your Items such as ListBox will display elements in order.

like image 68
ghord Avatar answered Jan 31 '26 09:01

ghord



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!