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 701011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:33:43+00:00 2026-05-14T03:33:43+00:00

I have a lot of comparer classes where the class being compared is simply

  • 0

I have a lot of comparer classes where the class being compared is simply checking the name property of the object and doing a string compare. For example:

public class ExerciseSorter : IComparer<Exercise>
{
    public int Compare(Exercise x, Exercise y)
    {
        return String.Compare(x.Name, y.Name);
    }
}

public class CarSorter : IComparer<Car>
{
    public int Compare(Car x, Car y)
    {
        return String.Compare(x.Name, y.Name);
    }
}

what is the best way to have this code generic so i dont need to write redundant code over and over again.

  • 1 1 Answer
  • 3 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-14T03:33:43+00:00Added an answer on May 14, 2026 at 3:33 am

    I use one like this:

    public class AnonymousComparer<T> : IComparer<T>
    {
        private Comparison<T> comparison;
    
        public AnonymousComparer(Comparison<T> comparison)
        {
            if (comparison == null)
                throw new ArgumentNullException("comparison");
            this.comparison = comparison;
        }
    
        public int Compare(T x, T y)
        {
            return comparison(x, y);
        }
    }
    

    Usage:

    var carComparer = new AnonymousComparer<Car>((x, y) => x.Name.CompareTo(y.Name));
    

    If you’re doing a straight property compare and the property type implements IComparable (for example an int or string), then, I also have this class which is a bit more terse to use:

    public class PropertyComparer<T, TProp> : IComparer<T>
        where TProp : IComparable
    {
        private Func<T, TProp> func;
    
        public PropertyComparer(Func<T, TProp> func)
        {
            if (func == null)
                throw new ArgumentNullException("func");
            this.func = func;
        }
    
        public int Compare(T x, T y)
        {
            TProp px = func(x);
            TProp py = func(y);
            return px.CompareTo(py);
        }
    }
    

    Usage of this one is:

    var carComparer = new PropertyComparer<Car, string>(c => c.Name);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have heard a lot that PHP is slow compared other languages. Is the
We have lot of object with this kind of design : Interface and several
Why do animation classes (like DoubleAnimation) require dependency properties? Dependency properties have a lot
Let's say i have a class like that: class Test { string asd; int
I have seen this in a lot of XML comments for classes in the
I have been doing a lot of reading on service layers and business layers
If I have a program that needs to compare a lot of strings, what's
I have lot of products with custom options, now I have requirement to update
I have lot of code in my Tornado app which looks like this: @tornado.web.asynchronous
I have a requirement to use date picker in my applicaiton. I have lot

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.