Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 813089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:18:15+00:00 2026-05-15T01:18:15+00:00

What purpose does the Comparer<T> class serve if the type that you specify already

  • 0

What purpose does the Comparer<T> class serve if the type that you specify already implements IComparable?

If I specify Comparer.Default, and Customer already implements IComparable, then why would I use the Comparer class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:18:16+00:00Added an answer on May 15, 2026 at 1:18 am

    I think your question is why have a base class that only seems to have one useful method which happens to be the same method you’d implement if you implemented the interface directly. If I understood that correctly I guess you’re right that there’s not much benefit to deriving from Comparer<T> as opposed to implementing IComparer<T> directly except that the base class provides you with a generic and non-generic implementation with a single overridden method.

    But if your question is why have both IComparer<T> and IComparable<T>, then
    as others have indicated, Comparer<T> allows you to define different ways to perform the comparison. It is an implementation of the Strategy design pattern. A great example of this would be the various StringComparer properties such as StringComparer.Ordinal, StringComparer.OrdinalIgnoreCase, etc. This allows you to sort strings differently under different circumstances which the IComparable<T> interface simply cannot anticipate.

    But in addition to being able to re-define the way a comparison is performed, sometimes providing an external comparer is the only way you can do it. For example, the Windows Forms ListView class allows you to specify an IComparer for its sort logic. But ListViewItem does not implement IComparable. So without a comparer strategy that knows how to do it, ListViewItem is not sortable because it has no default IComparable implementation.

    So at the end of the day, it’s just another extensibility point that allows you more flexibility when you’re not the author of the type that is to be sorted (or you need multiple sorting strategies.)


    EDIT: In response to your comment below: “if I have a class that implements IComparer (my own class), this would allow me to sort on any number of properties (a custom sort), why then would I bother using Comparer.Default”

    Perhaps an example would help. Let’s say you’re writing an extension method that checks to see if a given value is between a range.

    public static bool Between<T>(this T value, T minValue, T maxValue) {
    
        var comparer = Comparer<T>.Default;
    
        int c1 = comparer.Compare(value, minValue);
        int c2 = comparer.Compare(value, maxValue);
    
        return (c1 >= 0 && c2 <= 0);
    
    }
    

    In this case, I don’t know anything about the type T. It may implement IComparable or it may implement IComparable<T> or it may implement neither and an exception will be thrown. This also allows me to easily add an overload for this method that lets the caller pass in their own comparer. But Comparer comes in handy here because it lets me get a default comparer for an unknown type that may or may not implement a generic or non-generic IComparable interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does using then instead of semicolon or linebreak have only decorative purpose (to make
When creating a stored procedure, does the BEGIN/END block serve and purpose? eg, CREATE
The purpose of the following code is that when the user is holding the
I have a bug in my current code that I have been banging my
What does resolve_variable do? And could I use it for accessing the request outside
This code does not run properly, but it suggests what I am trying to
This is an interview question : Given a directory with lots of files, find
Our team is looking for alternative to ActiveX for our browser based client. So
i'm new at c.. and still having trouble at the syntax, hope you can
I was reading the source for ChanL the other day. It contains an example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.