IComparer Sorting multiple properties.
-
Hi Experts, I want to sort multiple properties in the collection using IComparer. I could explain below what exactly i am looking for. I want to sort FirstName By Ascending and LastName by Descending order and vice versa. I am not looking for below scenario. collection.Sort(FirstName, Ascending) collection.Sort(LastName, Descending) But i am looing for below scenario. collection.Sort(FirstName, Ascending, LastName, Descending). Please need help on this. Thanks. Vijay
-
Hi Experts, I want to sort multiple properties in the collection using IComparer. I could explain below what exactly i am looking for. I want to sort FirstName By Ascending and LastName by Descending order and vice versa. I am not looking for below scenario. collection.Sort(FirstName, Ascending) collection.Sort(LastName, Descending) But i am looing for below scenario. collection.Sort(FirstName, Ascending, LastName, Descending). Please need help on this. Thanks. Vijay
Hi, if you want to sort in a special way, you need to create a class that implements IComparer, which means it provides a method int Compare (Object x, Object y) where you first cast x and y to the appropriate class, then perform the comparison the way you want it. Example, assuming the collection holds instances of class Person:
class MyPersonComparer: IComparer {
int Compare(object x, object y) {
Person p1=(Person)x;
Person p2=(Person)y;
int diff=string.compare(p1.LastName, p2.LastName); // highest priority field first
if (diff==0) diff=string.compare(p2.FirstName, p1.FirstName); // reverse sort order
return diff;
}
}BTW: you could embed the Compare method inside an existing class (e.g. Person itself). :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, if you want to sort in a special way, you need to create a class that implements IComparer, which means it provides a method int Compare (Object x, Object y) where you first cast x and y to the appropriate class, then perform the comparison the way you want it. Example, assuming the collection holds instances of class Person:
class MyPersonComparer: IComparer {
int Compare(object x, object y) {
Person p1=(Person)x;
Person p2=(Person)y;
int diff=string.compare(p1.LastName, p2.LastName); // highest priority field first
if (diff==0) diff=string.compare(p2.FirstName, p1.FirstName); // reverse sort order
return diff;
}
}BTW: you could embed the Compare method inside an existing class (e.g. Person itself). :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
Thanks a lot Luc. This is exactly i was looking for. :)