472,958 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Help: ArrayList, Sort, Menu, IComparer, Object, multidemensional

Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional

I have a multi-dimensional arraylist, and I would like to sort one
level of it but not all. The multi-dimensional arraylist represents my
menu system, and I would like to sort the third level menu items only
and not the first two.

So, I've been trying to use some of the suggestions I've found here
about sorting. Mainly, I've created a comparison interface.

public class DiscountIDComparer : IComparer
{
public int Compare (object x, object y)
{
return string.CompareOrdinal((string)x, (string)y);
}

}

And I've been calling this by capturing the 3rd level of menus and
trying to implement the sort:

ArrayList menus = [loads here];
int i =0;
foreach(Menu menu in menus)
{
if (i == 2)
{
// I only want to sort the 3rd level menu items
IComparer foo = new DiscountIDComparer();
menu.Items.Sort(foo);
}
i++;
}

So, the problem is that what reaches the comparison interface is an
object and I cannot extract the 'text' from them to compare. Does any
of this make sense, or am I'm missing something. I feel that if I
could capture the text from the menu items that are passes through the
comparison interface then it would be ok, but alas I cannot seem to
find that. Any suggestions would be greatly appreciated.

Thank you.

Nov 3 '06 #1
3 2318

jt*****@eudoramail.com wrote:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional

I have a multi-dimensional arraylist, and I would like to sort one
level of it but not all. The multi-dimensional arraylist represents my
menu system, and I would like to sort the third level menu items only
and not the first two.

So, I've been trying to use some of the suggestions I've found here
about sorting. Mainly, I've created a comparison interface.

public class DiscountIDComparer : IComparer
{
public int Compare (object x, object y)
{
return string.CompareOrdinal((string)x, (string)y);
}

}

And I've been calling this by capturing the 3rd level of menus and
trying to implement the sort:

ArrayList menus = [loads here];
int i =0;
foreach(Menu menu in menus)
{
if (i == 2)
{
// I only want to sort the 3rd level menu items
IComparer foo = new DiscountIDComparer();
menu.Items.Sort(foo);
}
i++;
}

So, the problem is that what reaches the comparison interface is an
object and I cannot extract the 'text' from them to compare. Does any
of this make sense, or am I'm missing something. I feel that if I
could capture the text from the menu items that are passes through the
comparison interface then it would be ok, but alas I cannot seem to
find that. Any suggestions would be greatly appreciated.
There are some critical pieces of information missing, here, but I'll
try filling in the blanks and you can adapt the results to your
situation.

Let's say that the Menu.Items property returns an ArrayList of objects
of type MenuItem. That is, you could say something like

foreach (MenuItem item in menu.Items) { ... }

Furthermore, let's say that a MenuItem has a property called MenuText
of type string that is the menu text that appears to the user.

Given this, your IComparer needs to look like this:

public class DiscountIDComparer : IComparer
{
public int Compare (object x, object y)
{
itemX = (MenuItem)x;
itemY = (MenuItem)y;
return String.CompareOrdinal(itemX.MenuText, itemY.MenuText);
}
}

Nov 3 '06 #2
Thank you, that helped. I was just messing up the type cast... the
object was passed as a hyperlink. So, these modifications fixed the
problem, thanks again:

public class DiscountIDComparer : IComparer
{
public int Compare (object x, object y)
{
HyperLink HyperLnkX = (HyperLink)x;
HyperLink HyperLnkY = (HyperLink)y;

string strX = HyperLnkX.Text.ToString().Trim();
string strY = HyperLnkY.Text.ToString().Trim();

return string.CompareOrdinal(strX, strY);
}
}

Nov 3 '06 #3

jt*****@eudoramail.com wrote:
Thank you, that helped. I was just messing up the type cast... the
object was passed as a hyperlink. So, these modifications fixed the
problem, thanks again:

public class DiscountIDComparer : IComparer
{
public int Compare (object x, object y)
{
HyperLink HyperLnkX = (HyperLink)x;
HyperLink HyperLnkY = (HyperLink)y;

string strX = HyperLnkX.Text.ToString().Trim();
string strY = HyperLnkY.Text.ToString().Trim();

return string.CompareOrdinal(strX, strY);
}
}
You're welcome. By the way, HyperLink.Text is already a string, so
there's no need to call ToString() on it:

string strX = HyperLnkX.Text.Trim();

Nov 3 '06 #4

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

Similar topics

10
by: eLisHa | last post by:
i have an arraylist that gets it's values dynamiclly from the database, after paging, searching & more in the ASP.net file. What im trying to do, is sort the results that are sorted in the...
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
7
by: Derek Martin | last post by:
Hi there, I've asked before but never got an answer that could get me completely to where I was heading. I have an arraylist that contains defined objects and I would like to sort this arraylist...
9
by: Paul Nations | last post by:
I've got arraylists of simple classes bound to controls. I need to search through those arraylists to set the correct SelectedItem in the control. The code looks like: Public Class...
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...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
4
by: M Harvey | last post by:
I have an arraylist that contains datetime values. What is the best way to sort this arraylist by date ascending? Thanks, Matt
3
by: Stuart | last post by:
I am using Visual Basic 2005. I have created a two dimensional ArrayList named aSystem that is populated as follows:- aSystem.Add(New PickList(0, "Undefined")) aSystem.Add(New PickList(-1,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.