67

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

passenger.name
passenger.age
passenger.surname

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!

Thanks.

bombai
  • 927
  • 1
  • 17
  • 27

4 Answers4

150

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()
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Hi Guffa, thanks a lot! I have used the first case, and works, but this one order in ascendent order 10>12>15 if i want in descendent order how can I do it? Thanks a lot again. – bombai Jul 31 '12 at 08:33
  • 10
    @bombai: Just switch the variables in the comparison: `y.age.CompareTo(x.age)`. With the second method you use `OrderByDescending` instead. – Guffa Jul 31 '12 at 08:36
  • I struggled to changed my structure to implement `IComparer` until I found this! – Baby Jan 26 '15 at 03:45
  • @Guffa Do you know which is faster? – Felix Feb 26 '15 at 14:43
  • 1
    @Felix: Generally the speed is about the same. There are some differences that can affect performance in some edge cases. The first one sorts the list in place, so it uses less memory. The second one caches the value that it sorts on, so it will only call the function once for each item. – Guffa Feb 26 '15 at 14:57
  • 1
    Very helpful. Thanks for keeping it short and precise. – dhruvpatel Apr 21 '15 at 16:01
  • do not forget to add Imports System.Linq – user4951 Jun 14 '17 at 08:11
  • @Guffa How to sort by descending my declared variable: `myList as List(Of Integers)` . Thanks! – kiLLua Aug 16 '19 at 03:57
  • @Baby, the struggle was probably because you should have been implementing `IComparable` rather than `IComparer`. – jmcilhinney Dec 30 '19 at 10:57
  • 2
    It's important to understand that the first option sorts the existing list while the second option creates a new list. As a result, the second option is not a good choice if you may have other references to the existing list. – jmcilhinney Dec 30 '19 at 10:58
5

If you need a custom string sort, you can create a function that returns a number based on the order you specify.

For example, I had pictures that I wanted to sort based on being front side or clasp. So I did the following:

Private Function sortpictures(s As String) As Integer
    If Regex.IsMatch(s, "FRONT") Then
        Return 0
    ElseIf Regex.IsMatch(s, "SIDE") Then
        Return 1
    ElseIf Regex.IsMatch(s, "CLASP") Then
        Return 2
    Else
        Return 3
    End If
End Function

Then I call the sort function like this:

list.Sort(Function(elA As String, elB As String)
                  Return sortpictures(elA).CompareTo(sortpictures(elB))
              End Function)
user890332
  • 1,315
  • 15
  • 15
0

you must implement IComparer interface.

In this sample I've my custom object JSONReturn, I implement my class like this :

Friend Class JSONReturnComparer
    Implements IComparer(of JSONReturn)

    Public Function Compare(x As JSONReturn, y As JSONReturn) As Integer Implements    IComparer(Of JSONReturn).Compare
        Return String.Compare(x.Name, y.Name)
    End Function

End Class

I call my sort List method like this : alResult.Sort(new JSONReturnComparer())

Maybe it could help you

G. Gearing
  • 13
  • 1
  • 5
0

try..

Dim sortedList = From entry In mylist Order By entry.name Ascending Select entry

mylist = sortedList.ToList