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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:52:36+00:00 2026-05-30T21:52:36+00:00

I have a couple of classes that implement an interface, IFoo. I need to

  • 0

I have a couple of classes that implement an interface, IFoo. I need to display a list of objects of these classes in a table, which I’d like to be able to sort by any arbitrary column in the table. So, the data source for the table is a List<IFoo>.

The problem I’ve run into is that the standard way of implementing IComparable or IComparer for objects that are used in a list requires a static method, but static methods aren’t allowed in interfaces. So, the question boils down to this: how does one sort a List<IFoo>?

  • 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-30T21:52:37+00:00Added an answer on May 30, 2026 at 9:52 pm

    IComparable

    I don’t know what gives you the idea that you need to be using a static method, but it’s not correct.

    You can force all your IFoo implementers to implement IComparable<IFoo> by adding it to IFoo:

    interface IFoo : IComparable<IFoo> 
    { 
        int Value { get; set; } // for example's sake
    }
    
    class SomeFoo : IFoo
    {
        public int Value { get; set; }
    
        public int CompareTo(IFoo other)
        {
            // implement your custom comparison here...
    
            return Value.CompareTo(other.Value); // e.g.
        }
    }
    

    Then simply sort your List<IFoo> like so:

    list.Sort();
    

    Sorting by an arbitrary column

    You initially stated you want to sort by an arbitrary column in a table of IFoo objects. This is more complex; you need to be able to sort a list of objects by any one of their public properties, so the basic IComparable<IFoo> implementation above isn’t going to cut it.

    The solution is to create a PropertyComparer<T> class which implements IComparer<T>, and will sort by any property of T. You could write it specifically for IFoo, but at some point every developer tends to come up against this problem and ends up writing a generic property compararer that will try to sort any type. As a result you can google “c# property comparer” and you’re sure to get several hits. Here’s a simple one:

    http://www.codeproject.com/Articles/16200/Simple-PropertyComparer

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

Sidebar

Related Questions

I have a couple of classes BitMask & BitMaskLarge that both implement an interface
I have a need to create a couple of classes that will serve as
ok I have couple of .NET classes that I want to use in VBA.
I have a couple of classes that are singletons so I tried to create
I have a couple of classes that want to pass each other some information
I'm trying to implement some unit tests for a couple of classes that rely
I have a couple classes that are used only by a rake task. I
I have a really simple J2SE app that consists of a couple of classes.
I have couple resource DLLs that I currently load when application starts using following
I'll have couple of python functions I must interface with from the assembly code.

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.