How do I use sort for ArrayList here

T

Tony

Hello!

I have a class called Item as follows. I use CompareTo to be able to sort an
ArrayList containg Items on heatNumber. This works fine.
Now to my question. I also want to be able to sort a different ArrayList
containg Items on steelGrade. How is that done because I can only have one
CompareTo for the Item class.

class Item : IComparer
{
int heatNumber;
string steelGrade;

public int HeatNumber
{
set { heatNumber = value; }
get { return heatNumber; }
}


public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;
return this.HeatNumber - item.HeatNumber;
}
else
{
throw new ArgumentException("Object to compare is not a Item
object");
}
}
}

//Tony
 
A

Alberto Poblacion

Tony said:
I have a class called Item as follows. I use CompareTo to be able to sort
an
ArrayList containg Items on heatNumber. This works fine.
Now to my question. I also want to be able to sort a different ArrayList
containg Items on steelGrade. How is that done because I can only have one
CompareTo for the Item class.

class Item : IComparer
{
int heatNumber;
string steelGrade;

public int HeatNumber
{
set { heatNumber = value; }
get { return heatNumber; }
}


public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;
return this.HeatNumber - item.HeatNumber;
}
else
{
throw new ArgumentException("Object to compare is not a Item
object");
}
}
}

You can write a separate comparer in another class:

public class SteelGradeComparer : IComparer
{
public int Compare (Object x, Object y)
{
Item x1 = x as Item;
Item y1 = y as Item;
if (x1==null || y1==null)
throw new ArgumentException("Wrong type");
return string.Compare(x1.steelGrade, y1.steelGrade);
}
}
....
ArrayList myList = ....;
myList.Sort(new SteelGradeComparer());
 
T

Tim Jarvis

Tony said:
Hello!

I have a class called Item as follows. I use CompareTo to be able to
sort an ArrayList containg Items on heatNumber. This works fine.
Now to my question. I also want to be able to sort a different
ArrayList containg Items on steelGrade. How is that done because I
can only have one CompareTo for the Item class.

class Item : IComparer
{
int heatNumber;
string steelGrade;

public int HeatNumber
{
set { heatNumber = value; }
get { return heatNumber; }
}


public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;
return this.HeatNumber - item.HeatNumber;
}
else
{
throw new ArgumentException("Object to compare is not a
Item object");
}
}
}

//Tony

Hi Tony,

The answers you got are the correct ones for the question you asked, no
doubt, but.....

If you have the just the hint of the option of moving to C# 3.5 then I
strongly urge you to. There is just so much good stuff around
collections of data, that its just ridiculous, if you do any
aggregation of data at all, be it in XML, Object Lists or databases you
simply owe it to yourself to check it out.

So, in 3.5 you could use a linq extension method and simple lambda to
sort on any field you want without having to write any more comparers
for your class, you could...

alist.OfType<Item>().OrderBy(i => i.SteelGrade);

or better yet, use a generic list and save the casting call.

Regards Tim.


--
 
J

Jon Skeet [C# MVP]

If you have the just the hint of the option of moving to C# 3.5 then I
strongly urge you to.

I know this sounds like a nitpick, but I think it's worth the
community trying to get this right as early as possible: it's not C#
3.5 - there's no such thing. It's C# 3.0 (or C# 3 for short). If you
want LINQ, you need .NET 3.5 (or download LINQBridge and use C# 3
targeting .NET 2.0).

Jon
 

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