473,377 Members | 1,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

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.
Sep 16 '08 #1
6 3000
Plater
7,872 Expert 4TB
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.
Sep 16 '08 #2
Hi ,
I already do the following:
Expand|Select|Wrap|Line Numbers
  1. sortByCarMake = new SortByCarMake ();
  2.                 list.Sort(sortByCarMake);
  3.  
  4. public class SortByCarMake : IComparer
  5.     {
  6.         public SortByCarMake()
  7.         {
  8.         }
  9.  
  10.         int IComparer.Compare(Object x, Object y)
  11.         {
  12.             Car data1, data2;
  13.  
  14.            if (TypeCheck.Empty(x)
  15.                 || TypeCheck.Empty(y))
  16.             {
  17.                 return 0;
  18.             }
  19.             else
  20.             {
  21.                 data1 = (Car)x;
  22.                 data2 = (Car)y;
  23.  
  24.                 return data1.CarMake.CompareTo(data2.CarMake);
  25.             }
  26.         }
  27.     }
  28.  
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
Sep 16 '08 #3
Plater
7,872 Expert 4TB
Not knowing your object structure, I am just guessing at something like this:
Expand|Select|Wrap|Line Numbers
  1. int IComparer.Compare(Object x, Object y)
  2. {
  3.    Car data1, data2;
  4.    data1=(Car)x;
  5.    data2=(Car)y;
  6. if (data1.CarMake> data2.CarMake)
  7. {
  8.    return 1;//might be -1 depending on your logic
  9. }
  10. else if (data1.CarMake == data2.CarMake)
  11. {//continue heirachy logic
  12.    //repeat the CarMake logic for the .Year property in a similar fashion.
  13.    //inside the Year logic, create the logic for .ModelName
  14. }
  15. else
  16. {
  17.    return -1;//might be 1 depending on your logic
  18. }
  19.  
Sep 16 '08 #4
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.
Sep 16 '08 #5
I don't know how do continue the hierarchical sort logic. Please help.
Sep 16 '08 #6
Plater
7,872 Expert 4TB
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.
Sep 17 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: JezB | last post by:
I have a hashtable (h) of class objects keyed by a string (k) - the class (c) object stored in each hastable entry has two attributes (a1 and a2). I want to iterate through the entries of the...
2
by: awiklund | last post by:
Hi, I am sorting an arrayList using a custom comparer, but it sort the element in a uncommon fashion. Take a look at this code. using System; namespace ConsoleApplication8
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
0
by: stigbn | last post by:
When a DataSet is used as data source for a DataGrid one can get hiearachical datagrid by using relations among the DataTables of the DataSet. (By hierarchical datagrid, I mean columns that can be...
2
by: Rob G | last post by:
Hello, I have a basic understanding of Objects, inheritance, polymorhpism, etc. I am new to VB.NET and I am having a problem figuring this out. I want to sort a CheckBoxList. Using the...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
3
by: djp | last post by:
Hi I have to sort arraylist. I tried to do this using this page as a reference: http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm I did it exactly the same way but...
4
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.