473,705 Members | 3,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help: ArrayList, Sort, Menu, IComparer, Object, multidemensiona l

Re: ArrayList, Sort, Menu, IComparer, Object, multidemensiona l

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 DiscountIDCompa rer : IComparer
{
public int Compare (object x, object y)
{
return string.CompareO rdinal((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 DiscountIDCompa rer();
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 2376

jt*****@eudoram ail.com wrote:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensiona l

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 DiscountIDCompa rer : IComparer
{
public int Compare (object x, object y)
{
return string.CompareO rdinal((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 DiscountIDCompa rer();
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 DiscountIDCompa rer : IComparer
{
public int Compare (object x, object y)
{
itemX = (MenuItem)x;
itemY = (MenuItem)y;
return String.CompareO rdinal(itemX.Me nuText, 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 DiscountIDCompa rer : 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.CompareO rdinal(strX, strY);
}
}

Nov 3 '06 #3

jt*****@eudoram ail.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 DiscountIDCompa rer : 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.CompareO rdinal(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
8082
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 ArrayList, each time by another field. The ArrayList contains a list of a class which i built, that fits these results. I was told to use a IComparer with the sort method, after i try'ed just ArrayList1.Sort i got the following error:...
3
6448
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. Simple code: Dim y As New ArrayList y.Add("Abc") y.Add("abc") y.Add("_bc")
19
2452
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 would like to sort the arraylist based on object.name - is that possible? Thanks! Derek
7
1352
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 based on the values of one of the object's members. Here is a bit of code to illustrate my setup: Object construction and insertion: Dim entity As New entityobjects(entitynamenodelist(i).InnerXml, typenodelist(i).InnerXml, ...)
9
2872
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 DegreeMaintenance Private arrCipCodes As New ArrayList 'populate reader with data With rdr Do While .Read arrCipCodes.Add(New CipCode(.GetString(0), .GetString(1)))
16
6428
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 sorting (because it's ultimately being consumed in a GridView). I can't simply sort the ArrayList (because it only knows it's holding a bunch of objects). So I need a way to sort the ArrayList, based on the data - that is within the objects that...
48
4471
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 lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
4
13306
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
6971
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, "Standard UTM")) aSystem.Add(New PickList(-2, "UTM-NAD83")) aSystem.Add(New PickList(-3, "UTM-NAD27"))
0
8768
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8690
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6606
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2491
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.