【发布时间】:2009-10-23 20:25:50
【问题描述】:
在控件 ObjectListView(http://objectlistview.sourceforge.net/html/cookbook.htm) 中,我正在尝试添加一个自定义排序器,它忽略“The”和“A”前缀。
我设法使用常规的 ListView 来做到这一点,但现在我切换到 ObjectListView(更多的功能,而且更方便),我似乎无法做到这一点。
以下是我认为的 ObjectListView 代码中的主要比较器...
public int Compare(object x, object y)
{
return this.Compare((OLVListItem)x, (OLVListItem)y);
}
升序的原始排序器,例如(忽略“A”和“The”)
public class CustomSortAsc : IComparer
{
int IComparer.Compare(Object x, Object y)
{
string[] px = Convert.ToString(x).Split(' ');
string[] py = Convert.ToString(y).Split(' ');
string newX = "";
string newY = "";
for (int i = 0; i < px.Length; i++)
{
px[i] = px[i].Replace("{", "");
px[i] = px[i].Replace("}", "");
}
for (int i = 0; i < py.Length; i++)
{
py[i] = py[i].Replace("{", "");
py[i] = py[i].Replace("}", "");
}
if ((px[1].ToLower() == "a") || (px[1].ToLower() == "the"))
{
if (px.Length > 1)
{
for (int i = 2; i < px.Length; i++)
newX += px[i];
}
}
else
{
for (int i = 1; i < px.Length; i++)
newX += px[i];
}
if ((py[1].ToLower() == "a") || (py[1].ToLower() == "the"))
{
if (py.Length > 1)
{
for (int i = 2; i < py.Length; i++)
newY += py[i];
}
}
else
{
for (int i = 1; i < py.Length; i++)
newY += py[i];
}
return ((new CaseInsensitiveComparer()).Compare(newX, newY));
}
【问题讨论】:
-
你能补充更多信息吗,比如你得到什么样的结果,你如何将 CustomSortAsc 与列表视图挂钩等等?