bombai bombai - 1 year ago 99 Question

Sort a List of Object in VB.NET

I have a list of passengers(object) where it has a differents properties..

And I want to sort this list by age criterion, how can i do this?

I know in a list of integer/string List.Sort() works, but if is an object list, i dont know if its possible to sort by the value of a object property!


Answer Source

To sort by a property in the object, you have to specify a comparer or a method to get that property.

Using the List.Sort method:

theList.Sort(Function(x, y) x.age.CompareTo(y.age))

Using the OrderBy extension method:

theList = theList.OrderBy(Function(x) x.age).ToList()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download