VB.NET排序IEnumerable类

时间:2013-11-01 15:10:09

标签: .net vb.net linq sorting ienumerable

我想对实现IEnumerable的VB类进行排序。 我不想要一个新对象/ linq。 它必须保留为原始对象但已排序。 这是一个示例类。

Public Class Person
    Public Sub New(ByVal fName As String, ByVal lName As String)
        Me.firstName = fName
        Me.lastName = lName
    End Sub

    Public firstName As String
    Public lastName As String
End Class

Public Class People
    Implements IEnumerable(Of Person)

    Private _people() As Person

    Public Sub New(ByVal pArray() As Person)
        _people = New Person(pArray.Length - 1) {}

        Dim i As Integer
        For i = 0 To pArray.Length - 1
            _people(i) = pArray(i)
        Next i
    End Sub

    Public Function GetEnumerator() As IEnumerator(Of Person) _
            Implements IEnumerable(Of Person).GetEnumerator
        Return DirectCast(_people, IEnumerable(Of Person)).GetEnumerator
    End Function

    Private Function System_Collections_GetEnumerator() As IEnumerator _
            Implements IEnumerable.GetEnumerator
        Return Me.GetEnumerator
    End Function
End Class

我知道我可以使用LINQ,但后来我得到的新对象不是原来的排序。 有太多的地方使用全局“peopleList”对象,所以我需要对“peopleList”进行排序,而不是新的“People”排序列表。

Dim peopleList As New People({ _
    New Person("John", "Smith"), _
    New Person("Jim", "Johnson"), _
    New Person("Sue", "Rabon")})
Dim newPeopleList = From person In peopleList Order By person.firstName Select person
'OR
Dim newPeopleList = DirectCast(peopleList, IEnumerable(Of Person)).ToList()
newPeopleList.Sort(Function(p1, p2) p1.firstName.CompareTo(p2.firstName))

我需要更像以下内容。

Dim peopleList As New People({ _
    New Person("John", "Smith"), _
    New Person("Jim", "Johnson"), _
    New Person("Sue", "Rabon")})
peopleList = peopleList.Sort(Function(p) p.firstName)

我知道IEnumerable没有Sort方法。我只是把它作为一个例子来展示。 我可以选择修改“People”类,但最后它仍然必须实现IEnumerable,因为我不想打破当前的调用者。 此外,当前的调用者必须能够看到“peopleList”排序。

3 个答案:

答案 0 :(得分:6)

您可以在创建Person列表时对People个对象进行排序:

Public Sub New(ByVal pArray() As Person)
    _people = New Person(pArray.Length - 1) {}

    Dim i As Integer = 0
    For
    For Each person In pArray.OrderBy(Function(p) p.firstName)
        _people(i) = person
        i = i + 1
    Next i
End Sub

或者,您可以提供自己的方法,在内部数组创建后对其进行排序。这样的事情应该有效:

Public Class People
    Public Sub Sort(Of T As IComparable)(KeySelector As Func(Of Person, T))
        Array.Sort(_people, Function(x, y) KeySelector(x).CompareTo(KeySelector(y)))
    End Sub
End Class

你可以像这样打电话:

peopleList.Sort(Function(p) p.firstName)

答案 1 :(得分:1)

您可以对基础数组进行排序,并使用自定义IComparer来适应:

http://msdn.microsoft.com/en-us/library/bzw8611x.aspx

类似的东西:

public class PersonComparer: IComparer<Person>
{
    public int Compare(Person x, Person y)
    {            
        return x.FirstName.CompareTo(y.FirstName);
    }
}

然后在你的People课程中:

public sub Sort()

     PersonComparer pc = new PersonComparer()

    Array.Sort(_people, pc)

end sub

答案 2 :(得分:1)

您只需添加Sort方法:

Public Sub Sort()
    _people = _people.OrderBy(Function(p) p.firstname).ToArray()
End Sub

Public Sub Sort(Of TKey)(keySelector As Func(Of Person, TKey))
    _people = _people.OrderBy(keySelector).ToArray()
End Sub

或者在返回枚举器时进行排序:

Public Function GetEnumerator() As IEnumerator(Of Person) _
        Implements IEnumerable(Of Person).GetEnumerator
    Return _people.OrderBy(Function(p) p.firstname).GetEnumerator()
End Function