WhiskerBiscuit
WhiskerBiscuit

Reputation: 5157

Sorting a list of <Object> with VB and LINQ

I'm trying out some LINQ expressions and can't get them to work with the List class. Basically I want to be able to sort a list of custom objects by property type, however the C# LINQ syntax is KILLING me and I can't figure out how to convert it to VB

Class Foo
    Sub New(Name As String, Position As Integer)
        Me.Name = Name
        Me.Position = Position
    End Sub
    Public Name As String
    Public Position As Integer
End Class

Sub Main()
    Dim l As New List(Of Foo)
    l.Add(New Foo("C", 3))
    l.Add(New Foo("B", 2))
    l.Add(New Foo("A", 1))

    Dim asc = ..... (sort l by position asecnding)
    Dim desc = ..... (sort l by position descending)

End Sub

Upvotes: 10

Views: 40487

Answers (2)

harriyott
harriyott

Reputation: 10645

Dim asc = From f In l Order By f.Position
Dim desc = From f In l Order By f.Position Descending

Upvotes: 13

AnarchistGeek
AnarchistGeek

Reputation: 3186

I used c# to VB converter..

Dim sortedasc = l.OrderBy(Function(k) k.Position) 
Dim sorteddesc = l.OrderByDescending(Function(k) k.Position)

this should work..

Upvotes: 13

Related Questions