按对象属性对(对象)列表进行排序

2023-11-30

我正在努力实现一些已经给出答案的事情。但它在c#我对此一无所知c#所以我正在寻找 vb.net 的替代品。

我制造了一个class called BomItem它有几个属性,如数量、描述等。

我添加这些BomItems into a List(of BomItem)但现在我想根据属性对它们进行排序。如何根据项目对项目进行排序ItemNumber财产?

这里是link to the c#我找到的解决方案。

我的班级代码

Public Class BomItem
    Public Property ItemNumber As String
    Public Property Description As String
    Public Property Quantity As Double
    Public Property Material As String
    Public Property Certificate As String
End Class

我如何添加BomRow objects

    _NewBomList.Add(New BomItem() With {
                    .ItemNumber = oRow.ItemNumber,
                    .Description = oPropSet.Item("Description").Value,
                    .Quantity = oRow.TotalQuantity,
                    .Material = oPropSet.Item("Material").Value,
                    .Certificate = CustomPropertySet.Item("Cert.").Value})

Comparer

Public Class NaturalSort

Implements IComparer

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

    ' [1] Validate the arguments.
    Dim s1 As String = x
    If s1 = Nothing Then
        Return 0
    End If

    Dim s2 As String = y
    If s2 = Nothing Then
        Return 0
    End If

    Dim len1 As Integer = s1.Length
    Dim len2 As Integer = s2.Length
    Dim marker1 As Integer = 0
    Dim marker2 As Integer = 0

    ' [2] Loop over both Strings.
    While marker1 < len1 And marker2 < len2

        ' [3] Get Chars.
        Dim ch1 As Char = s1(marker1)
        Dim ch2 As Char = s2(marker2)

        Dim space1(len1) As Char
        Dim loc1 As Integer = 0
        Dim space2(len2) As Char
        Dim loc2 As Integer = 0

        ' [4] Collect digits for String one.
        Do
            space1(loc1) = ch1
            loc1 += 1
            marker1 += 1

            If marker1 < len1 Then
                ch1 = s1(marker1)
            Else
                Exit Do
            End If
        Loop While Char.IsDigit(ch1) = Char.IsDigit(space1(0))

        ' [5] Collect digits for String two.
        Do
            space2(loc2) = ch2
            loc2 += 1
            marker2 += 1

            If marker2 < len2 Then
                ch2 = s2(marker2)
            Else
                Exit Do
            End If
        Loop While Char.IsDigit(ch2) = Char.IsDigit(space2(0))

        ' [6] Convert to Strings.
        Dim str1 = New String(space1)
        Dim str2 = New String(space2)

        ' [7] Parse Strings into Integers.
        Dim result As Integer
        If Char.IsDigit(space1(0)) And Char.IsDigit(space2(0)) Then
            Dim thisNumericChunk = Integer.Parse(str1)
            Dim thatNumericChunk = Integer.Parse(str2)
            result = thisNumericChunk.CompareTo(thatNumericChunk)
        Else
            result = str1.CompareTo(str2)
        End If

        ' [8] Return result if not equal.
        If Not result = 0 Then
            Return result
        End If
    End While

    ' [9] Compare lengths.
    Return len1 - len2

End Function

End Class

Use LINQ OrderBy:

_NewBomList.OrderBy(Function(bi) bi.ItemNumber)

对于降序:

_NewBomList.OrderByDescending(Function(bi) bi.ItemNumber)

如果您想在字符串中使用数字顺序,则必须首先将其转换为整数:

_NewBomList.OrderBy(Function(bi) Integer.Parse(bi.ItemNumber))

Edit:
提供定制IComparer对于 OrderBy 扩展,您必须创建一个实现的类IComparer(Of String)String 是你的ItemNumbers比较:

 Class ItemNumberComparer  
     Implements IComparer(Of String)

     Public Function Compare(String x, String y)
         Dim ix As String() = x.Split("."C)
         Dim iy As String() = y.Split("."C)

         Dim maxLen As Integer = Math.Max(ix.Length, iy.Length)
         For i As Integer = 0 To maxLen - 2
             If ix.Length >= i AndAlso iy.Length >= i Then
                If Integer.Parse(ix(i)) < Integer.Parse(iy(i)) Then
                   Return -1 'If x.i is LT y.i it must be smaller at all
                ElseIf Integer.Parse(ix(i)) > Integer.Parse(iy(i)) Then
                   Return 1 'If x.i is GT y.i it must be bigger all
                End If
             End If
         Next
         'This code is only executed if x and y differ at last number or have different ´number of dots
        If ix.Length = iy.Length Then
            Return Integer.Parse(ix(ix.Length - 1)).CompareTo(Integer.Parse(iy(iy.Length - 1))) 'Only last number differs
       Else
           Return ix.Length.CompareTo(iy.Length) 'The number with more dots is smaller
       End If

     End Function     
  End Class

调用语法:

Dim comparer = new ItemNumberComparer()
_NewBomList.OrderByDescending(Function(bi) bi.ItemNumber, comparer)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

按对象属性对(对象)列表进行排序 的相关文章

随机推荐