Say I have a list of Person objects:
class person
{
int id;
string FirstName;
string LastName;
}
How would I sort this list by the LastName member?
List<Person> myPeople = GetMyPeople();
myPeople.Sort(/* what goes here? */);
Say I have a list of Person objects:
class person
{
int id;
string FirstName;
string LastName;
}
How would I sort this list by the LastName member?
List<Person> myPeople = GetMyPeople();
myPeople.Sort(/* what goes here? */);
Under C# 3.0 you can use the following linq statement:
var sortedEnumerable = myPeople.OrderBy( p => p.LastName );
A custom IComparer<person>
. If you are using c#3.0 and .net3.5, have a look into the LINQ Methods, espescially IEnumerable<T>.OrderBy()
.
One option is to write a comparer:
class LastNameComparer : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return String.Compare(x.LastName, y.LastName);
}
}
An then
myPeople.Sort(new LastNameComparer());
Person
may also implement IComparable<Person>
, in which case myPeople.Sort()
will suffice. However, you may want to sort by other properties on other places, so this isn't a general method; if you want to sort by ID
on another report you can write another IComparer
, but you can only have one IComparable<Person>.CompareTo(Person other)
method.
If you're feeling lazy, or sure you won't use it again, you can also use a lambda:
myPeople.Sort((p1, p2) => String.Compare(p1.LastName, p2.LastName));
As others have suggested, you can use a custom IComparer method to sort your list. If you're only doing it once, the easiest way is using an anonymous method like such:
List<Person> myPeople = GetMyPeople();
myPeople.Sort(delegate(Person one, Person two)
{
return one.LastName.CompareTo(two.LastName);
});
Alternatively, you can implement IComparable on your Person class, so that the class knows how to sort itself:
class Person : IComparable<Person>
{
int id;
string FirstName;
string LastName;
public int CompareTo(Person other)
{
return LastName.CompareTo(other.LastName);
}
}
List<T>.Sort
will sort the list in-place. If that's what you want, sweet: use the overload that takes a Comparison<T>
delegate:
List<Person> myPeople = GetMyPeople();
myPeople.Sort((x, y) => x.LastName.CompareTo(y.LastName));
If you don't want to modify the actual list but want to enumerate over its contents in a sorted order, then, as others have suggested, use the LINQ extension method OrderBy
(.NET 3.5 and up) instead. This method can take a simple Func<T, TKey>
selector and order by the keys which this selector... you know, selects.
List<Person> myPeople = GetMyPeople();
var orderedByLastName = myPeople.OrderBy(p => p.LastName);
Nothing needs to go inside your "Sort" method call if your class implements IComparable
.
Check this link for detail.
Code:
class person : IComparable<person>
{
int id;
string FirstName;
string LastName;
public int CompareTo(person other)
{
//If you want to sort on FirstName
return FirstName.CompareTo(other.FirstName);
}
}
example of ICompare
/// <summary>
/// Sort class on LastName Descending Order
/// </summary>
private class PersonLastNameDescending : IComparer<Person>
{
public int Compare(Person aX, Person aY)
{
return aY.LastName.CompareTo(aX.LastName);
} // end Compare(...)
} // end inner class PersonLastNameDescending
MyPeople.Sort(new PersonLastNameDescending());