J
jtfaulk
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.
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.