Vb.net 实现IComparer.Compare时颠倒了排序顺序

Vb.net 实现IComparer.Compare时颠倒了排序顺序,vb.net,winforms,listview,compare,icomparer,Vb.net,Winforms,Listview,Compare,Icomparer,我有点挠头了 我正在使用winforms构建一个业务应用程序,我正在使用大量的列表视图。我有一个特别的列表,当用项目填充时,可以根据项目的类型将项目的forecolor属性设置为红色或黑色。我想对其进行排序,以便使用IComparer的自定义实现将所有红色项目显示在列表的顶部。它可以工作,但反过来工作,因为所有红色项目都显示在底部。这是我的密码 '//Populate list For Each i In allStock.Where(Function(c) c.StockSta

我有点挠头了

我正在使用winforms构建一个业务应用程序,我正在使用大量的列表视图。我有一个特别的列表,当用项目填充时,可以根据项目的类型将项目的forecolor属性设置为红色或黑色。我想对其进行排序,以便使用IComparer的自定义实现将所有红色项目显示在列表的顶部。它可以工作,但反过来工作,因为所有红色项目都显示在底部。这是我的密码

    '//Populate list
    For Each i In allStock.Where(Function(c) c.StockStatusID <> 6)
        '//Add item, color in red if it is not contained within a certain date range
    Next

    '//Use my custom built IComparable implementing class(SortListByRedText) to sort items so that red items appear at the top.
    Dim sorter As New SortListByRedText()
    StockManagementList.ListViewItemSorter = sorter
    StockManagementList.Sort()

'//Custom IComparer
Public Class SortListByRedText
    Implements IComparer


Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare

    Dim item1 = CType(x, ListViewItem)
    Dim item2 = CType(y, ListViewItem)

    If item1.ForeColor = Color.Red And item2.ForeColor = Color.Red Then
        Return 0
    ElseIf item1.ForeColor = Color.Red And item2.ForeColor = Color.Black Then
        Return 1
    ElseIf item1.ForeColor = Color.Black And item2.ForeColor = Color.Red Then
        Return -1
    Else
        Return 0
    End If

End Function

End Class
”//填充列表
对于allStock.Where中的每个i(函数(c)c.StockStatusID 6)
“//添加项目,如果项目不包含在特定日期范围内,则以红色显示
下一个
“//使用自定义构建的IComparable实现类(SortListByRedText)对项目进行排序,以便红色项目显示在顶部。
作为新SortListByRedText()的Dim分拣机
StockManagementList.ListViewItemSorter=分拣机
StockManagementList.Sort()
'//自定义IComparer
公共类SortListByRedText
实现IComparer
公共函数比较(x为对象,y为对象)作为整数实现System.Collections.IComparer.Compare
Dim item1=CType(x,ListViewItem)
Dim item2=CType(y,ListViewItem)
如果item1.ForeColor=Color.Red,item2.ForeColor=Color.Red,则
返回0
ElseIf item1.ForeColor=Color.Red,item2.ForeColor=Color.Black
返回1
ElseIf item1.ForeColor=Color.Black和item2.ForeColor=Color.Red
返回-1
其他的
返回0
如果结束
端函数
末级

EDIT:我在比较器中将-1和1顺序颠倒过来作为一个修复,但我不喜欢在不理解为什么它会工作的时候进行修复。当然,返回-1的任何内容都应该发送到列表的底部,而不是顶部?

下面是您的操作方法。反转
-1
1
零件

Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare

    Dim item1 = CType(x, ListViewItem)
    Dim item2 = CType(y, ListViewItem)

    If item1.ForeColor = Color.Red And item2.ForeColor = Color.Red Then
        Return 0
    ElseIf item1.ForeColor = Color.Red And item2.ForeColor = Color.Black Then
        Return -1
    ElseIf item1.ForeColor = Color.Black And item2.ForeColor = Color.Red Then
        Return 1
    Else
        Return 0
    End If    
End Function

还要注意,您忽略了除黑色或红色以外的任何其他颜色。你可能需要考虑这一点。

我已经这样做了,因为在这种情况下这是显而易见的事情。我不明白的是,为什么排序方法将所有返回-1的对象放在列表的顶部而不是底部?因为这就是
IComparer
的工作原理。当返回
-1
时,表示
x
小于
y
。因此,较小的元素(这里的x)将移动到顶部而不是底部。明白了吗?是的,这是有道理的,从某种意义上说,我知道为什么,较小的数字默认位于列表的顶部,但另一方面,它似乎有点反直觉,因为返回的项目比我预期的要大,它出现在比小于的项目更大的位置。接受你的回答,谢谢讨论。