473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Writing a Better Comparison<T> for complex Ts

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
2 1301
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
11
by: Jamie Burns | last post by:
Hello, I just did a simple benchmark: for (xx=0;xx<100000;xx++) { rDerived* derived = dynamic_cast<rDerived*>(object); if (derived) derived->setValue(message.data.messageSetInt.value); } ...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
16
by: kerberoz | last post by:
which is faster? If strMessage <> String.Empty Then End If or If Not strMessage Is Nothing Then
2
by: Frank Millman | last post by:
Hi all I need to pass data between a server program and a client program, both written in Python. My first thought was to use xml. The server builds up an xml string, without using any xml...
11
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
4
by: Anastasios Hatzis | last post by:
I'm looking for a pattern where different client implementations can use the same commands of some fictive tool ("foo") by accessing some kind of API. Actually I have the need for such pattern for...
13
by: baltasarq | last post by:
Hi, there ! When I use dynamic_cast for a single object, I do something like: void foo(Base *base) { if ( dynamic_cast<Derived *>( base ) != NULL ) { ((Derived *) base)->do_something() } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.