我创建了一个新的基于 IComparer 的界面,允许您指定列和方向。我这样做只是因为我需要我的排序代码尽可能通用——我有两个需要像这样排序的网格,我不想维护两次代码。这是界面,很简单:
public interface IByColumnComparer : IComparer
{
string SortColumn { get; set; }
bool SortDescending { get; set; }
}
显然,如果您不担心保持通用性(您可能应该),那么这并不是绝对必要的。然后,我构建了一个基于 BindingList 的新类。这使我能够覆盖排序代码并逐列提供我自己的 IByColumnComparer,这是我需要的灵活性。看看这个:
public class SortableGenericCollection<T> : BindingList<T>
{
IByColumnComparer GenericComparer = null;
public SortableGenericCollection(IByColumnComparer SortingComparer)
{
GenericComparer = SortingComparer;
}
protected override bool SupportsSortingCore
{
get
{
return true;
}
}
protected override bool IsSortedCore
{
get
{
for (int i = 0; i < Items.Count - 1; ++i)
{
T lhs = Items[i];
T rhs = Items[i + 1];
PropertyDescriptor property = SortPropertyCore;
if (property != null)
{
object lhsValue = lhs == null ? null :
property.GetValue(lhs);
object rhsValue = rhs == null ? null :
property.GetValue(rhs);
int result;
if (lhsValue == null)
{
result = -1;
}
else if (rhsValue == null)
{
result = 1;
}
else
{
result = GenericComparer.Compare(lhs, rhs);
}
if (result >= 0)
{
return false;
}
}
}
return true;
}
}
private ListSortDirection sortDirection;
protected override ListSortDirection SortDirectionCore
{
get
{
return sortDirection;
}
}
private PropertyDescriptor sortProperty;
protected override PropertyDescriptor SortPropertyCore
{
get
{
return sortProperty;
}
}
protected override void ApplySortCore(PropertyDescriptor prop,
ListSortDirection direction)
{
sortProperty = prop;
sortDirection = direction;
GenericComparer.SortColumn = prop.Name;
GenericComparer.SortDescending = direction == ListSortDirection.Descending ? true : false;
List<T> list = (List<T>)Items;
list.Sort(delegate(T lhs, T rhs)
{
if (sortProperty != null)
{
object lhsValue = lhs == null ? null :
sortProperty.GetValue(lhs);
object rhsValue = rhs == null ? null :
sortProperty.GetValue(rhs);
int result;
if (lhsValue == null)
{
result = -1;
}
else if (rhsValue == null)
{
result = 1;
}
else
{
result = GenericComparer.Compare(lhs, rhs);
}
return result;
}
else
{
return 0;
}
});
}
protected override void RemoveSortCore()
{
sortDirection = ListSortDirection.Ascending;
sortProperty = null;
}
}
EDIT 这应该提供一些关于如何根据我上面的界面创建自己的 IComparer 的信息。 拥有自己的基于接口的 IComparer 的优点是您可以以一种方式对某些列进行排序,而对其他列进行另一种排序(有些列可能是字符串,有些可能是整数,有些可能对顶部的内容有特殊规则,等)。以下是您的 IComparer 可能如何工作的示例:
public class MyGenericComparer : IByColumnComparer
{
private string columnToCompare;
private bool descending;
public string SortColumn
{
get { return columnToCompare; }
set { columnToCompare = value; }
}
public bool SortDescending
{
get { return descending; }
set { descending = value; }
}
public MyGenericComparer(string column, bool descend)
{
columnToCompare = column;
descending = descend;
}
public int Compare(object x, object y)
{
MyGenericObject firstObj = (MyGenericObject )x;
MyGenericObject secondObj = (MyGenericObject )y;
if (descending)
{
MyGenericObject tmp = secondObj ;
secondObj = firstObj ;
firstObj = tmp;
}
if (columnToCompare == "StringColumn")
{
//Run code to compare strings, return the appropriate int
//eg, "1" if firstObj was greater, "-1" is secondObj, "0" if equal
}
if (columnToCompare == "IntColumn")
{
//Run code to compare ints, return the appropriate int
//eg, "1" if firstObj was greater, "-1" is secondObj, "0" if equal
}
}
}
那么您所要做的就是使用您的比较器实例创建您的列表!
public static MyGenericComparer GridComparer = new MyGenericComparer();
public static SortableGenericCollection<GenericObject> GridList = new SortableGenericCollection<GenericObject>(GridComparer);