Hierarchical sorting of class stored in arraylist using c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • O Champa
    New Member
    • Sep 2008
    • 4

    Hierarchical sorting of class stored in arraylist using c#

    Hi,

    I have code to store a custom class in an arraylist. CarMake, Year and CarModel are all properties inside the custom class Car for which data is stored in the arraylist. I need to sort the data according first based on CarMake. Then within CarMake the data needs to be sorted by Year and then within Year the data needs to be sorted by ModelName. Any help of how to do the above, will be greatly appreciated.

    Thanks a lot in advance.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You could either implement IComparer interface in something and pass it to the Sort() function, or have your custom class implement IComparable and the .Sort() function will use it automatically.
    You will need to code the logic into the correct function so it knows what order to sort.

    Comment

    • O Champa
      New Member
      • Sep 2008
      • 4

      #3
      Hi ,
      I already do the following:
      [code=#]
      sortByCarMake = new SortByCarMake ();
      list.Sort(sortB yCarMake);

      public class SortByCarMake : IComparer
      {
      public SortByCarMake()
      {
      }

      int IComparer.Compa re(Object x, Object y)
      {
      Car data1, data2;

      if (TypeCheck.Empt y(x)
      || TypeCheck.Empty (y))
      {
      return 0;
      }
      else
      {
      data1 = (Car)x;
      data2 = (Car)y;

      return data1.CarMake.C ompareTo(data2. CarMake);
      }
      }
      }
      [/code]

      That is the first level of sorting. I don't know how to sort based on Year based on what is returned above... As the arraylist that needs to be returned after 3 levels of sorting.

      Thanks

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Not knowing your object structure, I am just guessing at something like this:
        [code=c#]
        int IComparer.Compa re(Object x, Object y)
        {
        Car data1, data2;
        data1=(Car)x;
        data2=(Car)y;
        if (data1.CarMake> data2.CarMake)
        {
        return 1;//might be -1 depending on your logic
        }
        else if (data1.CarMake == data2.CarMake)
        {//continue heirachy logic
        //repeat the CarMake logic for the .Year property in a similar fashion.
        //inside the Year logic, create the logic for .ModelName
        }
        else
        {
        return -1;//might be 1 depending on your logic
        }
        [/code]

        Comment

        • O Champa
          New Member
          • Sep 2008
          • 4

          #5
          The Car class has CarMake, Year and CarModel as public string properties. However It would be great to find out how Year can be sorted by converting to a number. Also in your example CarMake sorting did not work for me as probably '>' was used. Any help will be appreciated a lot.

          Thanks.

          Comment

          • O Champa
            New Member
            • Sep 2008
            • 4

            #6
            I don't know how do continue the hierarchical sort logic. Please help.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              The logic for the other properties would be identical.
              You will have to decide what strings of CarMake take precendance over others.

              this is a pretty basic logic program, it should be fairly simple when you are complete with it.

              Comment

              Working...