locked
Writing a generic IComparer method. RRS feed

  • Question

  • User-1690009347 posted
    I have a set of objects in which I am implementing the IComparer interface. Currently I have to write a new IComparer class for each Property I want to sort by. I am trying to write a generic class which can be implemented in the base class and be accessed by each object which inherits from it. I would then like to be able to pass in the Property to the class so that it sorts by that Property instead of having a different class per property. I currently have the following code:- private class SortDateCreatedHelper<T> : IComparer { SortDirection _direction; int result = 0; public SortDateCreatedHelper(SortDirection Direction) { _direction = Direction; } int IComparer.Compare(object a, object b) { T f1 = (T)a; T f2 = (T)b; if (f1._dateCreated < f2._dateCreated) { result = -1; } else if (f1._dateCreated > f2._dateCreated) { result =1; } else { result= 0; } if (_direction == SortDirection.Descending) { if (result != 0) { //flip the result for descending. result = 0 - result; } } return result; } } This is called by the following method:- public static IComparer SortDateCreated(SortDirection Direction) { return (IComparer)new SortDateCreatedHelper<MyObject>(Direction); } The problem is that it complains that T does not have a property of _dateCreated. How do I pass in a property to this class and get it to sort on it? Or is there a better way of doing what I want to do?
    Wednesday, May 23, 2007 7:02 AM

Answers

  • User-1690009347 posted
    I have now sorted out the problem. I found that it is impossible to see the derived class from a static method of the base class so I am just passing the type in manually. It is a bit messy but it works fine. Thanks for all your help
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 24, 2007 10:54 AM

All replies

  • User-1611549905 posted

    Have you considered reflection? You would use something like this:

    object f1Value = f1.GetType().GetProperty(propertyName).GetValue(f1, null);

    Wednesday, May 23, 2007 9:08 AM
  • User-1690009347 posted
    I have managed to get reflection working but now have the problem that I am calling the class from a static method and I need to get the type of the class that the static method is in. Unfortunately I can't use 'this' in a static method so how do I go about getting the type so that I can pass it into the sorting class call?
    Wednesday, May 23, 2007 10:17 AM
  • User-1690009347 posted
    I have almost sorted out the problem. I can use MethodBase.GetCurrentMethod().DeclaringType but it does not quite work. The problem is that I have a base class called SortableObject and a class called Form. Form Inherits from SortableObject but the sorting methods and hence the MethodBase.GetCurrentMethod().DeclaringType call are in SortableObject. MethodBase.GetCurrentMethod().DeclaringType therefore returns SortableObject as the Type and not Form. Any ideas on how I get Form returned as the Type?
    Wednesday, May 23, 2007 11:08 AM
  • User1379789479 posted

    you're trying to against the concept of a static method. can you re-post the source code you're trying to fix? i'm having a hard time seeing your problem, but from a high-level standpoint, using that kind of reflection in a sorting routine is ill-advised at best.

    Wednesday, May 23, 2007 2:24 PM
  • User-1690009347 posted
    I have now sorted out the problem. I found that it is impossible to see the derived class from a static method of the base class so I am just passing the type in manually. It is a bit messy but it works fine. Thanks for all your help
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 24, 2007 10:54 AM