ListView.Sort() 无法与 IComparer C# 一起正常工作
ListView.Sort() not working correctly with IComparer C#
在我的代码中,我有一个名为 allocations 的 ListView,它将位置名称和选择优先级存储为它的列。目前,它看起来像这样:-
| Location | Picking Priority |
|----------------|------------------------|
| Location A | 1000 |
| Location B | 750 |
| Location C | 1000 |
我正在尝试使用 IComparer 按拣选优先级排序,以便拣选优先级最低的位置位于列表顶部,其余位置按升序排列。目前,这是我的代码:-
public ArrayList getSortedListView()
{
ListView lvLocations = new ListView();
lvLocations.ListViewItemSorter = new ListViewItemComparer();
// Reads CSV file to get required location.
// lvGlobalLocations is filled with every location on the system.
foreach (ListViewItem item in lvGlobalLocations.Items)
{
if (item.Text == <location name>)
{
lvLocations.Items.Add((ListViewItem)item.Clone());
lvLocations.Sort();
}
}
// Cycles through ListView and stores location names in ArrayList to be returned.
}
class ListViewItemComparer : IComparer
{
public int Compare(object a, object b)
{
ListViewItem lvi1 = (ListViewItem)a;
ListViewItem lvi2 = (ListViewItem)b;
int int1 = int.Parse(lvi1.SubItems[1].Text);
int int2 = int.Parse(lvi2.SubItems[1].Text);
if (int1 > int2)
return -1;
else if (int1 < int2)
return 1;
else
return 0;
}
}
我怎样才能让它正确比较,以便返回的 ArrayList 的顺序正确?
其实你不需要那么多。 Lamda 允许在排序中指定 属性。只需确保您正在定义 "sub item" 或了解对象的签名。
var sorted = list.OrderByDescending(item => item.PropertyToSortBy);
如果要求是使用比较器对象,则必须实际使用它。
var sorted = list.Sort(comparitor);
它将使用对象本身定义的默认排序。这表明了另一种可能性。包含的对象是 ICompariable
.
同样,我会简单地使用 lamda 表达式。这是一个微不足道的排序。一旦您必须对众多属性和其他处理逻辑进行排序,您会考虑使用比较器或将排序逻辑放在对象本身上。
如上所述,您应该永远不要使用ArrayList。今天是 2016 年 2 月 24 日,请停止踢 .Net 1.1 军团。
您可以使用 LINQ 实现此目的:
public IEnumerable<ListViewItem> getSortedListView()
{
ListView lvLocations = new ListView();
lvLocations.ListViewItemSorter = new ListViewItemComparer();
// Reads CSV file to get required location.
// lvGlobalLocations is filled with every location on the system.
return lvGlobalLocations.Items.Where(item => item.Text == <location name>).OrderBy(x => x.Whatever);
}
或者使用List.Sort
(是的,泛型列表有很多有用的方法):
public List<ListViewItem> getSortedListView()
{
ListView lvLocations = new ListView();
lvLocations.ListViewItemSorter = new ListViewItemComparer();
// Reads CSV file to get required location.
// lvGlobalLocations is filled with every location on the system.
var list = new List<ListViewItem>(lvGlobalLocations.Items.Where(item => item.Text == <location name>))
list.Sort((a, b) => a.Whatever.CompareTo(b.Whatever));
return list;
}
在我的代码中,我有一个名为 allocations 的 ListView,它将位置名称和选择优先级存储为它的列。目前,它看起来像这样:-
| Location | Picking Priority |
|----------------|------------------------|
| Location A | 1000 |
| Location B | 750 |
| Location C | 1000 |
我正在尝试使用 IComparer 按拣选优先级排序,以便拣选优先级最低的位置位于列表顶部,其余位置按升序排列。目前,这是我的代码:-
public ArrayList getSortedListView()
{
ListView lvLocations = new ListView();
lvLocations.ListViewItemSorter = new ListViewItemComparer();
// Reads CSV file to get required location.
// lvGlobalLocations is filled with every location on the system.
foreach (ListViewItem item in lvGlobalLocations.Items)
{
if (item.Text == <location name>)
{
lvLocations.Items.Add((ListViewItem)item.Clone());
lvLocations.Sort();
}
}
// Cycles through ListView and stores location names in ArrayList to be returned.
}
class ListViewItemComparer : IComparer
{
public int Compare(object a, object b)
{
ListViewItem lvi1 = (ListViewItem)a;
ListViewItem lvi2 = (ListViewItem)b;
int int1 = int.Parse(lvi1.SubItems[1].Text);
int int2 = int.Parse(lvi2.SubItems[1].Text);
if (int1 > int2)
return -1;
else if (int1 < int2)
return 1;
else
return 0;
}
}
我怎样才能让它正确比较,以便返回的 ArrayList 的顺序正确?
其实你不需要那么多。 Lamda 允许在排序中指定 属性。只需确保您正在定义 "sub item" 或了解对象的签名。
var sorted = list.OrderByDescending(item => item.PropertyToSortBy);
如果要求是使用比较器对象,则必须实际使用它。
var sorted = list.Sort(comparitor);
它将使用对象本身定义的默认排序。这表明了另一种可能性。包含的对象是 ICompariable
.
同样,我会简单地使用 lamda 表达式。这是一个微不足道的排序。一旦您必须对众多属性和其他处理逻辑进行排序,您会考虑使用比较器或将排序逻辑放在对象本身上。
如上所述,您应该永远不要使用ArrayList。今天是 2016 年 2 月 24 日,请停止踢 .Net 1.1 军团。
您可以使用 LINQ 实现此目的:
public IEnumerable<ListViewItem> getSortedListView()
{
ListView lvLocations = new ListView();
lvLocations.ListViewItemSorter = new ListViewItemComparer();
// Reads CSV file to get required location.
// lvGlobalLocations is filled with every location on the system.
return lvGlobalLocations.Items.Where(item => item.Text == <location name>).OrderBy(x => x.Whatever);
}
或者使用List.Sort
(是的,泛型列表有很多有用的方法):
public List<ListViewItem> getSortedListView()
{
ListView lvLocations = new ListView();
lvLocations.ListViewItemSorter = new ListViewItemComparer();
// Reads CSV file to get required location.
// lvGlobalLocations is filled with every location on the system.
var list = new List<ListViewItem>(lvGlobalLocations.Items.Where(item => item.Text == <location name>))
list.Sort((a, b) => a.Whatever.CompareTo(b.Whatever));
return list;
}