Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 120 characters in body
Source Link
Guffa
  • 703k
  • 111
  • 760
  • 1k

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

For a non-LINQ solution:

You need a named method as a comparer:

theList.Sort( Private Function Comparer(ByVal x As Product, ByVal y As Product) =>As {Integer Dim result intAs resultInteger = x.Popular.CompareTo(y.Popular);   ifIf (result === 0) {Then   result = x.Clicked.CompareTo(y.Clicked);   ifIf (result === 0) {Then result = x.Name.CompareTo(y.Name); End }If   End }If   returnReturn result;result  End }Function 

Usage:

theList.Sort(AddressOf Comparer); 

For a non-LINQ solution:

theList.Sort(  (x, y) => { int result = x.Popular.CompareTo(y.Popular);   if (result == 0) {   result = x.Clicked.CompareTo(y.Clicked);   if (result == 0) { result = x.Name.CompareTo(y.Name); }   }   return result;   } ); 

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

For a non-LINQ solution:

You need a named method 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) 
Source Link
Guffa
  • 703k
  • 111
  • 760
  • 1k

For a non-LINQ solution:

theList.Sort( (x, y) => { int result = x.Popular.CompareTo(y.Popular); if (result == 0) { result = x.Clicked.CompareTo(y.Clicked); if (result == 0) { result = x.Name.CompareTo(y.Name); } } return result; } );