Код: Выделить всё
List
200906/1
200906/2
..
200906/10
200906/11
200906/12
Мой список привязан к
Код: Выделить всё
BindingList
Код: Выделить всё
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
что в данном случае неприятно.
Теперь я хочу использовать для этого свой собственный IComparer. Выглядит это так:
Код: Выделить всё
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]);
}
}
}
Код: Выделить всё
case ListSortDirection.Ascending:
MyComparer comparer = new MyComparer();
items = items.OrderByDescending(
x => property.GetValue(x), comparer).ToList();
break;
Но мой список по-прежнему отсортирован «неправильным» способом. Я что-то упускаю? Понятия не имею.
Подробнее здесь: https://stackoverflow.com/questions/985 ... nq-orderby
Мобильная версия