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

时间:2016-08-17 09:40:58

标签: vb.net list sorting properties

我试图获得已经给出答案的东西。但是它出现在c#中,而且我对c#没有任何了解,所以我正在寻找一个vb.net替代方案。

我制作了一个名为class的{​​{1}},其中包含数量,描述等属性。

我将这些BomItem添加到BomItems中,但现在我想根据属性对它们进行排序。如何根据List(of BomItem)属性对项目进行排序?

以下是我找到的ItemNumber解决方案的link

我的班级代码

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

的Comparer

    _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})

3 个答案:

答案 0 :(得分:3)

使用LINQ OrderBy

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

和降序:

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

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

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

修改
要为OrderBy扩展提供自定义IComparer,您必须创建一个实现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)

答案 1 :(得分:2)

来自其他线程的C#代码:

List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();

等同于这个VB代码:

List(Of Order) SortedList = objListOrder.OrderBy(Function(o) o.OrderDate).ToList()

如您所见,变化很小。您只需要知道泛型和lambda表达式的语法。

但是,您应该知道,此代码不会对您的列表进行排序。它对列表中的项目进行排序,然后按顺序将它们添加到新列表中。这对于许多应用程序来说都非常好,但如果您在其他地方使用相同的列表,那么您将无法在那里看到新订单。虽然有一些变化,但实际对列表进行排序的一种方法是:

objListOrder.Sort(Function(o1, o2) o1.OrderDate.CompareTo(o2.OrderDate))

答案 2 :(得分:0)

另一种解决方案是实现IComparablesee MSDN ref)接口。此界面旨在以自定义方式对对象进行排序:

Public Class BomItem
    Implements IComparable

    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

    Public Function CompareTo(obj As Object) As Integer
         Dim bom = CType(obj, BomItem)

         If Not bom Is Nothing then
             Return Me.ItemNumber.CompareTo(bom.ItemNumber)
         Else
             Throw New ArgumentException("Object is not a BomItem")
         End If
End Class

您可以这样对列表进行排序:

Dim myList As New List(Of BomItem)

'Code to add your BomItems

myList.Sort()

这实际上会对您的列表进行排序,但不会创建新列表。