I have List I want to sort Desc by Priority, which is int and can be anything from 0 to 100.
I use GenericComparer, which allows to set the SortDirection and sortExpression. Works well for any string property, like a Name (which is property of ProductLowDetail). However for Priority it behaves strange. It does sort, but it changes the direction of sorting (from Desc to Asc and then from Asc to Desc and that is repeated) for each reload of List (simple F5 in browser).
I store List in session and below id code how I call it.
productList.Sort(new GenericComparer<ProductLowDetail>("DisplayPriority", SortDirection.Descending));
Part of comparer implementation:
public int Compare(T x, T y){
PropertyInfo propertyInfo = typeof (T).GetProperty(_sortExpression);
IComparable obj1 = (IComparable) propertyInfo.GetValue(x, null);
IComparable obj2 = (IComparable) propertyInfo.GetValue(y, null);
if (SortDirection == SortDirection.Ascending){
return obj1.CompareTo(obj2);
}
return obj2.CompareTo(obj1);
}
Has someone get into this problem too? thanks for any suggestion. X.