【发布时间】:2026-01-18 00:05:01
【问题描述】:
我有一个包含以下值的网格
工艺美术
文化
保龄球
科学技术
苹果 - 太棒了
苹果 - 健康
苹果 - 兴趣
苹果 - 营养素
我正在尝试使用比较器,但无法使其正常工作。请帮忙。
Private Function Compare (ByVal x as Object, ByVal y as Object) as Integer Implements Compare
Dim xCell As String = CType(x, String)
Dim yCell As String = CType(y, String)
Dim x1, y1 As Integer
Dim xReturnVal As String = String.Empty
Dim xCollection As MatchCollection = Regex.Matches(xCell, "^IAPPLE\s")
For Each m As Match In xCollection
xReturnVal += If(m.ToString() = "", "0", "1")
Next
If Not xReturnVal = "" Then
x1 = Convert.ToInt32(xReturnVal)
End If
Dim yReturnVal As String = String.Empty
Dim yCollection As MatchCollection = Regex.Matches(xCell, "^(?!IAPPLE).+")
For Each mt As Match In yCollection
yReturnVal += If(mt.ToString() = "", "0", "1")
Next
If Not yReturnVal = "" Then
y1 = Convert.ToInt32(yReturnVal)
End If
'Return yReturnVal.CompareTo(xReturnVal)
Return y1.CompareTo(x1)
End Function
【问题讨论】:
-
我认为您在这里不需要正则表达式。如果只有一个字符串以 APPLE 结尾,则返回 1 或 -1。然后,只需比较两个字符串。
-
你能给我一个更正的sn-p吗?
-
您可以使用
Linq将数据拆分成两个List,分别排序后再加入。 -
嗨,默认情况下,我按 alpha 顺序排列。当用户点击排序时,这就是我需要对其进行排序的时候。你能给我提供一个使用 IComparer 的工作 sn-p 吗?
标签: c# asp.net wpf vb.net icomparer