By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,894 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,894 IT Pros & Developers. It's quick & easy.

Writing a Better Comparison<T> for complex Ts

P: n/a
Hello:

I find myself writing Comparison<Tdelegates that looks like this:

private static int compareCustomers(Customer lhs, Customer rhs)
{
int result = Comparer<string>.Default.Compare(lhs.LastName,
rhs.LastName);
if (result == 0)
{
result = Comparer<string>.Default.Compare(lhs.FirstName,
rhs.FirstName);
}
return result;
}

Is this pretty standard?

This approach is fine up to the point where you have to compare more
than two or three fields.

Does anyone have a more elegant method for writing complex
Comparison<T>'s?

Thanks,
Travis
Jul 1 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
je**********@gmail.com <je**********@gmail.comwrote:
I find myself writing Comparison<Tdelegates that looks like this:

private static int compareCustomers(Customer lhs, Customer rhs)
{
int result = Comparer<string>.Default.Compare(lhs.LastName,
rhs.LastName);
if (result == 0)
{
result = Comparer<string>.Default.Compare(lhs.FirstName,
rhs.FirstName);
}
return result;
}

Is this pretty standard?

This approach is fine up to the point where you have to compare more
than two or three fields.

Does anyone have a more elegant method for writing complex
Comparison<T>'s?
In MiscUtil (http://pobox.com/~skeet/csharp/miscutil) we have a
ProjectionComparer and an extension method, so you can say:

IComparer<Customercomparer =
ProjectionComparer.Create(cust =cust.LastName)
.ThenBy(ProjectionComparer.Create(cust =cust.FirstName));
etc

This could probably be beefed up even further with a bit of effort, to
be honest.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 1 '08 #2

P: n/a
In MiscUtil (http://pobox.com/~skeet/csharp/miscutil) we have a
ProjectionComparer and an extension method, so you can say:

IComparer<Customercomparer =
* * ProjectionComparer.Create(cust =cust.LastName)
* * * *.ThenBy(ProjectionComparer.Create(cust =cust.FirstName));
etc

This could probably be beefed up even further with a bit of effort, to
be honest.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon_skeet
C# in Depth:http://csharpindepth.com- Hide quoted text -

- Show quoted text -
Stuck back in my 2.0 world, I have written this before:

private delegate int lameComparison();

private static int customerComparer(Customer lhs, Customer rhs)
{
lameComparison[] comparisons = {
delegate() { return
Comparer<string>.Default.Compare(lhs.LastName, rhs.LastName); },
delegate() { return
Comparer<string>.Default.Compare(lhs.FirstName, rhs.FirstName); },
};
int result = 0;
for (int i = 0; i != comparisons.Length && result == 0; ++i)
{
result = comparisons[i]();
}
return result;
}

However, this not only makes my coworkers froth at the mouth, it has
performance hits (minimal).

One more reason to move to 3.0.

Thanks,
Travis
Jul 1 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.