473,385 Members | 1,356 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,385 software developers and data experts.

Sorting an ArrayList.

Dear All,

I have been staring at this for ages now and I still can't see why it
doesn't work. Any help will be GREATLY appreciated.

I am simply trying to sort an arraylist using my own instance of
IComparer. Code below...

Quite simply it doesn't work. Can anyone see where I'm going wrong?

Thanks in advance,
Jose

<code>
public class TabDetail
{
public int ID;
public string Name;
public int Order;
public string URL;
}

public class TabOrderComparer: IComparer
{
public int Compare(object o1, object o2)
{
int SortID1 = ((TabDetail)o1).SortID;
int SortID2 = ((TabDetail)o2).SortID;

return SortID1.CompareTo(SortID2);
}
}
public class TabSettings
{
public ArrayList Tabs;
public int ActiveTab;

public TabSettings()
{
this.Tabs = new ArrayList();

PopulateTabs();

this.Tabs.Sort(new TabOrderComparer());
}

private void PopulateTabs()
{
Classes.DataAccessor oDA = new Classes.DataAccessor();
DataTable dtTabs = oDA.ReadTabs();

foreach(DataRow drTab in dtTabs.Rows)
{
TabDetail oTab = new TabDetail();

oTab.ID = (int)drTab["ID"];
...
//Populate the rest of the properties here...
}
}
}
</code>

Nov 19 '05 #1
2 854
Works for me. I had to modify your code a bit to (a) have a SortID and (b)
populate it from a source other than your database, I ended up with:

private void PopulateTabs()
{
for (int i = 0; i < 100; ++i)
{
TabDetail oTab = new TabDetail();
oTab.ID = rand.Next();
oTab.SortID = oTab.ID;
Tabs.Add(oTab);
}
}

where rand was just a random number generator:
public class TabSettings
{
private Random rand = new Random();
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Jose" <di*********@avandis.co.uk> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Dear All,

I have been staring at this for ages now and I still can't see why it
doesn't work. Any help will be GREATLY appreciated.

I am simply trying to sort an arraylist using my own instance of
IComparer. Code below...

Quite simply it doesn't work. Can anyone see where I'm going wrong?

Thanks in advance,
Jose

<code>
public class TabDetail
{
public int ID;
public string Name;
public int Order;
public string URL;
}

public class TabOrderComparer: IComparer
{
public int Compare(object o1, object o2)
{
int SortID1 = ((TabDetail)o1).SortID;
int SortID2 = ((TabDetail)o2).SortID;

return SortID1.CompareTo(SortID2);
}
}
public class TabSettings
{
public ArrayList Tabs;
public int ActiveTab;

public TabSettings()
{
this.Tabs = new ArrayList();

PopulateTabs();

this.Tabs.Sort(new TabOrderComparer());
}

private void PopulateTabs()
{
Classes.DataAccessor oDA = new Classes.DataAccessor();
DataTable dtTabs = oDA.ReadTabs();

foreach(DataRow drTab in dtTabs.Rows)
{
TabDetail oTab = new TabDetail();

oTab.ID = (int)drTab["ID"];
...
//Populate the rest of the properties here...
}
}
}
</code>

Nov 19 '05 #2
Thanks for your reply Karl.

Sorry about the code, I just copied a selection and obviously missed
one or two important things!! :-)

I'll have another look....

Jose

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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
6
by: Bryon | last post by:
I need to sort an ArrayList of objects. I am unable to find a method for this. DO I need to role my own sorting? Or is there something like the qsort() function in C of old??? Thanks
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...
2
by: Rob Meade | last post by:
Dear all, I have a class which contains an arraylist populated with other objects, for example: PrescriptionQueue - containing multiple instances of Prescription I have the need on my web...
16
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this:...
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...
12
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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.