# [ACCEPTED]-Sort Generic list on two or more values-lambda

Score: 40

EDIT Just realized this was a VB question. Here 2 is the VB.Net solution

``````Dim list = GetSomeList()
Dim sorted = list. _
OrderBy(Function(x) x.Popular). _
ThenBy(Function(x) x.Clicked). _
ThenBy(Function(x) x.Name)
``````

C# version. Try the 1 following

``````var list = GetSomeList();
var sorted = list.OrderBy(x => x.Popular).ThenBy(x => x.Clicked).ThenBy(x => x.Name);
``````
Score: 6

To answer your question about a lambda expression, that 4 is too complex to put in a lambda expression, as 3 VB doesn't support multi-line lambda expressions.

For 2 a non-LINQ solution:

You need a named method 1 as a comparer:

``````Private Function Comparer(ByVal x As Product, ByVal y As Product) As Integer
Dim result As Integer = x.Popular.CompareTo(y.Popular)
If result = 0 Then
result = x.Clicked.CompareTo(y.Clicked)
If result = 0 Then
result = x.Name.CompareTo(y.Name)
End If
End If
Return result
End Function
``````

Usage:

``````theList.Sort(AddressOf Comparer)
``````
Score: 5
``````List<Product> sortedProducts = null;
sortedProducts = products.OrderBy(p => p.Popular)
.ThenByDescending(p => p.Clicked)
.ThenBy(p => p.Name)
.ToList();
``````

0

Score: 4

I'm sorry but do you know any C#?

``````products.OrderBy(p => p.Popular).
ThenByDescending(p => p.Clicked).
ThenBy(p => p.Name);
``````

Can you 1 get what you need from this?

Score: 0

A compound sort can also be done with the 2 List.Sort lambda function. Here is a vb.Net 1 example:

``````    Dim Conts As List(of clsContact)
Conts.Sort(Function(C1 As clsContact, C2 As clsContact)
Dim CompRes As Integer = C1.Contact_LastName.CompareTo(C2.Contact_LastName)
If CompRes = 0 Then
CompRes = C1.Contact_FirstName.CompareTo(C2.Contact_FirstName)
End If

Return CompRes
End Function)
``````

More Related questions