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

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.
 
B

Bruce Wood

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);
}
}
 
J

jtfaulk

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);
}
}
 
B

Bruce Wood

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();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top