Fix Code in code example???
I am trying to use this guy's code but I don't know enough on how to fix it - because he was a little lazy in typing it.
from here>
http://blog.codewrench.net/2009/04/1...rary-property/
code that needs fixing
Code:
public enum SortDirection
{
Ascending, Descending
}
public class ListSorter where T : class
{
public static List Sort
(
List listToSort,
string propertyName,
SortDirection direction) where P : IComparable
{
Type propertyType = typeof (P);
Type comparableInterface = propertyType.GetInterface("IComparable");
if (comparableInterface == null)
throw new Exception("Properties to sort by must be IComparable");
listToSort.Sort(
delegate(T x, T y)
{
PropertyInfo p1 = x.GetType().GetProperty(propertyName, propertyType);
PropertyInfo p2 = y.GetType().GetProperty(propertyName, propertyType);
object p1objvalue = p1.GetValue(x, null);
object p2objvalue = p2.GetValue(y, null);
P p1value = (P)p1objvalue;
P p2value = (P)p2objvalue;
if (direction == SortDirection.Ascending)
return p1value.CompareTo(p2value);
else
return p2value.CompareTo(p1value);
});
return listToSort;
}
}
I was able to fix a small part of it, namely here >>
Code:
public class ListSorter<T> where T : class
but I can't solve the rest
Re: Fix Code in code example???
Fix what exactly? what doesn't work? You should expect us to do the legwork for you, narrow the problem description down.