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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:42:01+00:00 2026-05-29T15:42:01+00:00

I am not able to find out as to how to implement the IComparable

  • 0

I am not able to find out as to how to implement the IComparable interface method CompareTo for a generic class.

I have a class called BindingProperty<T> which is used to create a List<BindingProperty<intOrString>> to bind to a DataGrid. The problem is that the I can not perform the Sort operation as the IComparable interface is not implemented by this BindingProperty<T> class. The result of comparison would depend on a data member ‘Value’ of the the BindingProperty<T> class where ‘Value’ is of Type T. When I click on the column header of the DataGrid, I get an exception that CompareTo() method is not implemented.

I would need help to implement this interface. Should I be using IComparable<T>? If yes, how do I do it?

Thanks in advance
Shakti

  • 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-29T15:42:03+00:00Added an answer on May 29, 2026 at 3:42 pm

    Set up a generic type constraint on T.

    public class BindingProperty<T> : IComparable where T : IComparable
    {
        public T Value {get; set;}
    
        public int CompareTo(object obj)
        {
           if (obj == null) return 1;
    
           var other = obj as BindingProperty<T>;
           if (other != null) 
                return Value.CompareTo(other.Value);
           else
                throw new ArgumentException("Object is not a BindingProperty<T>");
        }
    }
    

    Edit:
    Alternative solution to handle if value does not implement IComparable. This will support all types, just wont sort if they do not implement IComparable.

    public class BindingProperty<T> : IComparable
        {
            public T Value {get; set;}
    
            public int CompareTo(object obj)
            {
               if (obj == null) return 1;
    
               var other = obj as BindingProperty<T>;
               if (other != null) 
               {
                   var other2 = other as IComparable;
                   if(other2 != null)
                      return other2.CompareTo(Value);
                   else
                      return 1; //Does not implement IComparable, always return 1
               }
               else
                    throw new ArgumentException("Object is not a BindingProperty<T>");
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is a simple question to which I am not able to find the
I have tried rectifying the code below. But I am not able to find
I have a very simple issue, but I am not able to find the
I am not able to find a way to set TransactionIsolation in ejb. Can
I am not able to find the reason of a crash in my iphone
I am not sure why but jQuery find function is not able to find
I did a lot of Googling and was not able to find a PERFECT
I'm getting an error with FlexUnit 4 that I am not able to find
I am trying to create a customized UISearchBar. I am not able to find
I am using 960.gs for designing website layouts. I could not able to find

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.