Vladut
Vladut

Reputation: 657

How to order/sort a collection into vb.net?

I want to sort/order a collection into Visual Basic, but for the moment with no luck. The main criterion is to not change the collection into another shape, like List or Dictionary.

My code:

    Dim colCols As Collection
    Dim FinalCollection As New Collection

    colCols = New Collection
    colCols.Add(2572, "MyID")
    FinalCollection.Add(colCols)

    colCols = New Collection
    colCols.Add(2576, "MyID")
    FinalCollection.Add(colCols)

    colCols = New Collection
    colCols.Add(2573, "MyID")
    FinalCollection.Add(colCols)

I want to order this collection by MyID, and in the final order to be like that: 2572, 2573 and 2576.

Note: FinalCollection is exactly in this shape and is impossible to change that code because is not into my application. So, FinalCollection is received from another application and I want to order the collection into my application.

I try this but with no luck:

Dim sortedList = From item In FinalCollection Order By item.Value("MyID")

Upvotes: 0

Views: 445

Answers (1)

G3nt_M3caj
G3nt_M3caj

Reputation: 2685

This might help you. (Even I’m not agree with this approach)

Dim newCollection As IOrderedEnumerable(Of Object) = From item In FinalCollection
                                                         Order By CType(item, Collection).Item("MyID")


For Each item As Collection In newCollection
    Console.WriteLine("Order of : " & item.Item("MyID").ToString)
Next

Upvotes: 1

Related Questions