473,387 Members | 1,502 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,387 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 2337

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: 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: 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
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...
0
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,...
0
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...

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.