By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,114 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,114 IT Pros & Developers. It's quick & easy.

problem with using CompareTo when having string

P: n/a
Hello!

I have method CompareTo written below. But I have a problem because I want
to sort on HeatNumber which is of type int and if this is the same on
MspName
which is a string. But according to the compiler I'm not allowed to use < on
string.
So my question is how do I solve this problem. The object Item is stored in
an ArrayList.

public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;
if (this.HeatNumber == item.HeatNumber) // same chargenumber
{
if (this.MspName < item.MspName)
return -1;
else if (this.MspName == item.MspName) // same sop
return 0;
else
return 1;
}
else
{
return this.HeatNumber - item.HeatNumber;
}
}
else
{
throw new ArgumentException("Object to compare is not a Item
object");
}
}

//Tony
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Tony,

Would you be looking for something like:

public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;

if (this.HeatNumber != item.HeatNumber)
return this.HeatNumber.CompareTo(item.HeatNumber);
else
return this.MspName.CompareTo(item.MspName);
}
else
{
throw new ArgumentException("Object to compare is not a Item object");
}
}

By the way. Consider using a List<Iteminstead of arraylist and have Item
implement IComparable<Item>. That way you don't have to check and cast the
object.

--
Happy Coding!
Morten Wennevik [C# MVP]
"Tony" wrote:
Hello!

I have method CompareTo written below. But I have a problem because I want
to sort on HeatNumber which is of type int and if this is the same on
MspName
which is a string. But according to the compiler I'm not allowed to use < on
string.
So my question is how do I solve this problem. The object Item is stored in
an ArrayList.

public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;
if (this.HeatNumber == item.HeatNumber) // same chargenumber
{
if (this.MspName < item.MspName)
return -1;
else if (this.MspName == item.MspName) // same sop
return 0;
else
return 1;
}
else
{
return this.HeatNumber - item.HeatNumber;
}
}
else
{
throw new ArgumentException("Object to compare is not a Item
object");
}
}

//Tony
Jun 27 '08 #2

P: n/a
On Wed, 28 May 2008 04:26:00 -0700, Morten Wennevik [C# MVP]
<Mo************@hotmail.comwrote:
[...]
By the way. Consider using a List<Iteminstead of arraylist and have
Item
implement IComparable<Item>. That way you don't have to check and cast
the
object.
And personally, even if the OP doesn't take that advice, I wouldn't bother
with the "is" and "throw". Just cast the object. The
InvalidCastException is IMHO just as informative as the explicitly thrown
one in the code example. :)

Pete
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.