Writing a Better Comparison<T> for complex Ts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jehugaleahsa@gmail.com

    Writing a Better Comparison<T> for complex Ts

    Hello:

    I find myself writing Comparison<Tdel egates that looks like this:

    private static int compareCustomer s(Customer lhs, Customer rhs)
    {
    int result = Comparer<string >.Default.Compa re(lhs.LastName ,
    rhs.LastName);
    if (result == 0)
    {
    result = Comparer<string >.Default.Compa re(lhs.FirstNam e,
    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
  • Jon Skeet [C# MVP]

    #2
    Re: Writing a Better Comparison&lt;T &gt; for complex Ts

    jehugaleahsa@gm ail.com <jehugaleahsa@g mail.comwrote:
    I find myself writing Comparison<Tdel egates that looks like this:
    >
    private static int compareCustomer s(Customer lhs, Customer rhs)
    {
    int result = Comparer<string >.Default.Compa re(lhs.LastName ,
    rhs.LastName);
    if (result == 0)
    {
    result = Comparer<string >.Default.Compa re(lhs.FirstNam e,
    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
    ProjectionCompa rer and an extension method, so you can say:

    IComparer<Custo mercomparer =
    ProjectionCompa rer.Create(cust =cust.LastName)
    .ThenBy(Project ionComparer.Cre ate(cust =cust.FirstName ));
    etc

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

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon_skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • jehugaleahsa@gmail.com

      #3
      Re: Writing a Better Comparison&lt;T &gt; for complex Ts

      In MiscUtil (http://pobox.com/~skeet/csharp/miscutil) we have a
      ProjectionCompa rer and an extension method, so you can say:
      >
      IComparer<Custo mercomparer =
          ProjectionCompa rer.Create(cust =cust.LastName)
             .ThenBy(Project ionComparer.Cre ate(cust =cust.FirstName ));
      etc
      >
      This could probably be beefed up even further with a bit of effort, to
      be honest.
      >
      --
      Jon Skeet - <sk...@pobox.co m>
      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 customerCompare r(Customer lhs, Customer rhs)
      {
      lameComparison[] comparisons = {
      delegate() { return
      Comparer<string >.Default.Compa re(lhs.LastName , rhs.LastName); },
      delegate() { return
      Comparer<string >.Default.Compa re(lhs.FirstNam e, rhs.FirstName); },
      };
      int result = 0;
      for (int i = 0; i != comparisons.Len gth && 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

      Comment

      Working...