Code: Select all
List
200906/1
200906/2
..
200906/10
200906/11
200906/12
Meine Liste ist an einen
gebunden
Code: Select all
BindingList
Code: Select all
protected override void ApplySortCore(
PropertyDescriptor property, ListSortDirection direction)
{
_sortProperty = property;
_sortDirection = direction;
var items = this.Items;
switch (direction)
{
case ListSortDirection.Ascending:
items = items.OrderByDescending(x => property.GetValue(x)).ToList();
break;
case ListSortDirection.Descending:
items = items.OrderByDescending(x => property.GetValue(x)).ToList();
break;
}
this.Items = items;
}
200906/1
200906/10
200906/11
200906/12
200906/2
was in diesem Fall unangenehm ist.
Jetzt möchte ich meinen eigenen IComparer damit verwenden. Es sieht so aus:
Code: Select all
public class MyComparer : IComparer
{
public int Compare(Object stringA, Object stringB)
{
String[] valueA = stringA.ToString().Split('/');
String[] valueB = stringB.ToString().Split('/');
if(valueA .Length != 2 || valueB .Length != 2)
return String.Compare(stringA.ToString(), stringB.ToString());
if (valueA[0] == valueB[0])
{
return String.Compare(valueA[1], valueB[1]);
}
else
{
return String.Compare(valueA[0], valueB[0]);
}
}
}
Code: Select all
case ListSortDirection.Ascending:
MyComparer comparer = new MyComparer();
items = items.OrderByDescending(
x => property.GetValue(x), comparer).ToList();
break;
Aber meine Liste ist immer noch „falsch“ sortiert. Vermisse ich etwas? Ich habe keine Ahnung.
Mobile version