【问题标题】:Sort a list based on an item inside the list that is another list根据列表中另一个列表的项目对列表进行排序
【发布时间】:2019-02-16 19:49:45
【问题描述】:

正如标题所说,但它变得更加复杂。这是一些示例代码

class person
prop name as string
prop age as int
prop properties as List(of ExtraProps)

class ExtraProps
prop key as string
prop value as string

所以说我想根据对象 ExtraProps.value 对班级人员列表进行排序,其中 Key = "name"

请注意,我正在使用 vs2005 和 .NET 2.0 版

【问题讨论】:

  • 请看stackoverflow.com/questions/11735902/…。您可以实现自己的比较逻辑,这应该满足您对自定义比较逻辑的要求
  • @Capricorn 我明白了。但是排序对我来说有点复杂,因为我需要它基于列表中的列表。我仍然在这个项目的 vs2005 中,因为它是我的工作,我不能因为我想改变项目的东西。

标签: vb.net list sorting visual-studio-2005 .net-2.0


【解决方案1】:

例如

Private Function ComparePersonsByExtraPropsName(p1 As Person, p2 As Person) As Integer
    Return GetFirstExtraProp(p1.Properties, "Name").Value.CompareTo(GetFirstExtraProp(p2.Properties, "Name").Value)
End Function

Private Function GetFirstExtraProp(properties As IEnumerable(Of ExtraProps), key As String) As ExtraProps
    For Each ep As ExtraProps In properties
        If ep.Key = key Then
            Return ep
        End If
    Next

    Return Nothing
End Function

然后:

Dim personList As New List(Of Person)

'...

personList.Sort(AddressOf ComparePersonsByExtraPropsName)

【讨论】:

  • .NET 2 请求
  • 当使用这个时,我在功能部分出现错误。它说预期表达。
  • 抱歉,我确实阅读了 .NET 2.0 部分,但很快就忘记了。我已将代码编辑为在 .NET 2.0 中应该可以使用的代码。
  • @TimSchmelter 你的答案消失了吗?
【解决方案2】:

首先我要感谢@TimSchmelter 的回答。这在 .NET 2.0 和 VS2005 中完美运行。

Public Class TaskKeyComparer
Implements IComparer(Of Trimble_Planning.TaskData)

Private ReadOnly _keyComparison As StringComparison
Private ReadOnly _valueComparison As StringComparison
Private ReadOnly _key As String

Public Sub New(ByVal key As String, Optional ByVal keyComparison As StringComparison = StringComparison.CurrentCulture, Optional ByVal valueComparison As StringComparison = StringComparison.CurrentCulture)
    _key = key
    _keyComparison = keyComparison
    _valueComparison = valueComparison
End Sub

Public Function Compare(ByVal x As person, ByVal y As person) As Integer Implements IComparer(Of person).Compare
    If x Is Nothing AndAlso y Is Nothing Then Return 0
    If x Is Nothing OrElse y Is Nothing Then Return CInt(IIf(x Is Nothing, -1, 1))
    If x.properties Is Nothing AndAlso y.properties Is Nothing Then Return 0
    If x.properties Is Nothing OrElse y.properties Is Nothing Then Return CInt(IIf(x.properties  Is Nothing, -1, 1))

    Dim xKeyValue As String = Nothing
    Dim yKeyValue As String = Nothing
    For Each prop As ExtraProps In x.properties 
        If String.Equals(prop.key, _key, _keyComparison) Then
            xKeyValue = prop.value
            Exit For
        End If
    Next
    For Each prop As ExtraProps In y.properties 
        If String.Equals(prop.key, _key, _keyComparison) Then
            yKeyValue = prop.value
            Exit For
        End If
    Next
    Return String.Compare(xKeyValue, yKeyValue, _valueComparison)
End Function
End Class

然后像这样使用它

personList.Sort(New TaskKeyComparer("name"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2011-03-22
    相关资源
    最近更新 更多