471,594 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 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 2246

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Derek Martin | last post: by
7 posts views Thread by Derek Martin | last post: by
9 posts views Thread by Paul Nations | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
4 posts views Thread by M Harvey | last post: by
3 posts views Thread by Stuart | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.