I have a listbox
DropPrice
MyPrice
Price1
Price2
I want to sort it like this
Price1
Price2
DropPrice
MyPrice
I mean, if there's an item that starts with the sequence "price", it gets priority, else the smallest string should get the priority.
My source code:
var lcv = (ListCollectionView)(CollectionViewSource.GetDefaultView(_itemsSource));
var customSort = new PrioritySorting("price");
lcv.CustomSort = customSort;
internal class PrioritySorting : IComparer
{
private string _text;
public PrioritySorting(string text)
{
_text = text;
}
public int Compare(object x, object y)
{
//my sorting code here
}
}
How can i write compare method. I know, that it can return 1,0 or -1. How can i set priorities.
You just have to check if it starts with "price".
Note that I don't think that
ToString()
is appropriate; you should rather implementIComparer<T>
and strongly type your objects in your listbox.