我创建了一个基于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;
}
}
编辑 这应该提供有关如何基于上面的接口创建自己的 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")
{
}
if (columnToCompare == "IntColumn")
{
}
}
}
然后,您只需使用比较器的实例创建列表即可!
public static MyGenericComparer GridComparer = new MyGenericComparer();
public static SortableGenericCollection<GenericObject> GridList = new SortableGenericCollection<GenericObject>(GridComparer);