How to sort/filter a Tlist by 2 or more columns - Support - .netTiers - CodeSmith Community
Welcome to the CodeSmith Community!

How to sort/filter a Tlist by 2 or more columns

.netTiers

A description has not yet been added to this group.

How to sort/filter a Tlist by 2 or more columns

Answered (Verified) This question is answered

Hi!!

I use sometimes the sort method of theTlist class to order the records presented to the user when they click on the column headers on a DataGridView. But sometimes I need to sort these TList's by 2 or more columns after a GetAll call just to show the data ordered to the user or for a report. I know these can be achieved directly and fasted with a custom sp, but what if I don't want to use custom proc's? huhuhu.

I know there is an interface like IComparer than can be used in other overloads of the sort method to perform this kind of sort, but I don't know how to use it.Tongue Tied

How can I perform this kind of sorting?Hmm

Thanks in advance.Yes

Noé

  • Post Points: 35
Verified Answer
  • These examples are completely off the top of my head so if the syntax is incorrect forgive me.

    Example 1 (Using string)
    private static void SortTest()
    {
      Tlist<Car> cars = new TList<Car>();
      string sortString = string.Concat(CarColumn.Make.ToString(), ", ", CarColumn.Model.ToString()); // "Make, Model"
      cars.Sort(sortString);
    }

    Example 2 (Using private method)


    private static void SortTest()
    {
      Tlist<Car> cars = new TList<Car>();
      cars.Sort(SortByDisplayName);
    }

    private static int SortByDisplayName(CarEntity x, CarEntity y)
    {
      int result = x.Make.CompareTo(y.Make);
      if (result != 0)
        return result;
               
      result = x.Model.CompareTo(y.Model);
      if (result != 0)
        return result;
    }

    Example 3 (Using IComparer)

    private static void SortTest()
    {
      Tlist<Car> cars = new TList<Car>();
      cars.Sort(new CarSorter());
    }

    public class CarSorter : IComparer
    {
      private static int Compare(object x, object y)
      {
        Car carX = x as Car;
        Car carY = y as Car;

        if (carX == null && carY == null)
          return 0;

        if (carX == null)
          return -1;

        if (carY == null)
          return 1;

        int result = x.Make.CompareTo(y.Make);
        if (result != 0)
          return result;
               
        result = x.Model.CompareTo(y.Model);
        if (result != 0)
          return result;
      }
    }

All Replies
  • I think you may be confused by an Interface so allow me to try and explain.

    An interface specifies that a Property or Method by a certain name must exist.

    So when you create your own class and inherit from the IComparer interface.

    The IComparer interface specifies that the following method must exist in the class.

    public int Compare(object x, object y)
    {
    }

    You're welcome to put whatever else you want into that class but whileever it implements that interface the Compare method must exist.

  • Hi Ryan!!

    Thanks for your answer. I have to say that I already know how to use (or implement) an Interface, it's just that I didn't know how to use this (IComparer), and as you said, I have to improve your (kindly posted) code 'cause there are some issues in it, as for example there isn't a default returned value when the value of a comparison returns 0 (you validate result !=0) and if I compile, compiler throws an error because of this (functions must have a returning value if they're not void).

    But of course, I'm very grateful by your help, because your code is very clear and easy to understand, besides that you shared it with me (and the whole community) and I got the idea. Now I just have to do my tests.

    Thanks again and best regards...Yes

    Noé

Page 1 of 1 (4 items)