score:0
Try
Public Class PeopleComparer
Implements IComparer(Of People)
Public Function Compare(x As People, y As People) As Integer
Dim lnameComparison As Integer = x.LName.CompareTo(y.LName)
Return If(lnameComparison = 0, x.FName.CompareTo(y.FName), lnameComparison)
End Function
End Class
and
lstPeople.Sort(New PeopleComparer())
score:1
Is this possible?
Yes, just write a comparer that implements the ordering that you want. So compare the last name first; if they are not equal return the result of CompareTo
and if they are not equal return the comparison between the first names.
score:1
Yeah it's possible.
I think the best way, if you can change the "People" class, create you own CompareTo()
function.
Private Function CompareTo(p2 As People) As Integer
Dim i As Int32 = Me.LName.CompareTo(p2.LName)
If i = 0 Then
Return Me.FName.CompareTo(p2.FName)
End If
Return i
End Function
then use it :
lstPeople.Sort(Function(p1, p2) p1.CompareTo(p2))
EDIT : Convert to VB.NET.
score:0
Implements System.Collections.Generic.IComparer(Of People).Compare
have to be added to the function. Stub is generated by typing enter key after IComparer(Of People)
Public Class PeopleComparer
Implements IComparer(Of People)
Public Function Compare(x As People, y As People) As Integer Implements System.Collections.Generic.IComparer(Of People).Compare
Dim lnameComparison As Integer = x.LName.CompareTo(y.LName)
Return If(lnameComparison = 0, x.FName.CompareTo(y.FName), lnameComparison)
End Function
End Class
score:0
Bala R's answer is essentially correct, but I had to provide the compiler with a little more information to get past the compiler error you were seeing:
Public Class PeopleComparer
Implements IComparer(Of People)
Public Function Compare(x As People, y As People) As Integer Implements IComparer(Of People).Compare
Dim lnameComparison As Integer = x.LName.CompareTo(y.LName)
Return If(lnameComparison = 0, x.FName.CompareTo(y.FName), lnameComparison)
End Function
End Class
and
lstPeople.Sort(New PeopleComparer())
Source: stackoverflow.com
Related Articles
- Does this LINQ code perform multiple lookups on the original data?
- How to reuse a linq expression for 'Where' when using multiple source tables
- multiple Sorts with generic memberexpressions
- LINQ Source Code Available
- multiple orderby in this linq code
- .NET 4 Code Contracts: "requires unproven: source != null"
- multiple sorts on list<Of T>
- creating Linq to sqlite dbml from DbLinq source code
- Code Cleanup: Best way to split long statement over multiple lines
- C# linq multiple sorts in one clause
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- C# SQL/Linq/Entity Framework calculating column totals for multiple columns from large data source
- Accessing a List multiple times in same code block - Any better approach?
- How do i delete Multiple Row of Record in this code
- Cleaning up multiple IF statements in the code
- source code for LINQ 101 samples
- How to sort list on multiple properties in one line of code in Linq
- Code First CTP: Multiple PKs or FKs
- map one to one with multiple primary key columns entity framework code first
- Writing code that can work with multiple database objects
- List or Array of String Contain specific word in Html Source Code
- Doing multiple sorts across more than one line of code?
- c# Linq or code to extract groups from a single list of source data
- Check if multiple sub lists are present in a source list
- How do I minimize C#, LINQ code in Deleting Multiple Records
- How to use pagelist with data source as multiple tables, to display table values in mvc?
- Multiple "order by" in LINQ
- Group By Multiple Columns
- C# Linq Group By on multiple columns
- Convert string[] to int[] in one line of code using LINQ
- Calling stored procedure from Entity Framework in C#
- Using C# Linq find and return a string value from a multi-level data store
- What is the purpose of a Key in Linq
- How to convert an IGroupedObservable to IGrouping?
- Grouping Data with counts
- Newtonsoft JSON.NET Deserialization error
- Order by, distinct and select top 5 results with linq
- Order string contains number (Linq, C#)
- How to include sorted navigation properties with Entity Framework
- C#: If condition in Linq where clause
- Linq Distinct() or GroupBy() method is not working in below scenario
- Can I control what the .NET compiler automatically converts to expressions
- Insert multiple rows to SQL from DataSet using LINQ
- InvalidOperationException Occurred in Linq orderby clause
- When returning the count from SQL, how to also include the value in grouped item for which the count doesn't exist?
- How to use Linq Query in Object Data Source?
- let statement and outer join in LINQ to SQL
- C# get difference in months?
- adding where clauses to linq query
- C# how to yield return SelectMany?