How do I sort collection items by value in VB.NET?
I want to sort this:
Dim col as Collection = New Collection
col.Add("b","b1")
col.Add("a","a1")
col.Add("d","d1")
How do I sort collection items by value in VB.NET?
I want to sort this:
Dim col as Collection = New Collection
col.Add("b","b1")
col.Add("a","a1")
col.Add("d","d1")
Like @Krishnadditya mentioned, Collections aren't ideal for sorting because they contain items of type Object
which is too genereic to be useful in comparing against each other. If you weren't married to a collection, you can do this with a LINQ query to a list or anything that can be cast an enumerable
Dim list = {
New With {.Object = "b", .Key = "b1"},
New With {.Object = "a", .Key = "a1"},
New With {.Object = "d", .Key = "d1"}}
Dim sortedList = _
From item In list
Order By item.Key