通过其中一个属性对对象的arraylist进行排序?

时间:2016-01-12 00:05:07

标签: vb.net arraylist

嗨,我有一个名为item的对象,这里是代码:

Public Class item
Private itemValue As String
Private urlfoundValue As String
Private typeValue As String
Private scorevalue As String
Public Sub New()
    ' Leave fields empty. 
End Sub

Public Sub New(ByVal item As String, ByVal urlfound As String, ByVal type As String, ByVal score As String)
    itemValue = item
    urlfoundValue = urlfound
    typeValue = type
    scorevalue = score
End Sub

Public Property item() As String
    Get
        Return itemValue
    End Get
    Set(ByVal value As String)
        itemValue = value
    End Set
End Property

Public Property urlfound() As String
    Get
        Return urlfoundValue
    End Get
    Set(ByVal value As String)
        urlfoundValue = value
    End Set
End Property

Public Property type() As String
    Get
        Return typeValue
    End Get
    Set(ByVal value As String)
        typeValue = value
    End Set
End Property

Public Property score() As String
    Get
        Return scorevalue
    End Get
    Set(ByVal value As String)
        scorevalue = value
    End Set
End Property

我有一个这些项的ArrayList,我想通过Score属性按从高到低的顺序排序(是字符串,但可以转换为int32)。

这些项目都存储在这个arraylist中:

Public fullitem As New System.Collections.ArrayList()

有关如何按分数获得物品的任何建议?也可以在c#中完成。

1 个答案:

答案 0 :(得分:1)

直接回答你的问题是使用这样的LINQ:

Reset to Empty

这使用Dim orderdList = fullitem.Cast(Of item)() _ .OrderBy(Function(i As item) Convert.ToInt32(i.score)) fullitem = New System.Collections.ArrayList() For Each item In orderdList fullitem.Add(item) Next Cast获取通用IEnumerable<item>,然后您可以使用ArrayList等LINQ函数。

但是,请注意OrderBy现已过时,您应该考虑使用ArrayList。如果你这样做,那么你可以做一些简单的事情:

假设您的列表定义如下:

List<Item>

你可以这样排序:

Dim list = new List(Of item)()