Connecting Tech Pros Worldwide Help | Site Map

String Comparison in "CompareTo" Method

Fir5tSight
Guest
 
Posts: n/a
#1: Aug 23 '06
Hi All,

I have an interface class defined as follows:

class FileName : IComparable
{
public FileName(string fileName, string packageName)
{
//
// TODO: Add constructor logic here
//
class FileName : IComparable
{
public FileName(string fileName, string packageName)
{
//
// TODO: Add constructor logic here
//
this.fileName = fileName;
this.packageName = packageName;

// missing unless visited to set to true
this.bVisited = false;
}

// Define how FileName objects are sorted
public int CompareTo(object other)
{
FileName otherFileObj = (FileName)other;
if (this.packageName < otherFileObj.packageName)
{
return (int)(this.packageName -
otherFileObj.packageName);
}
else if (this.packageName == otherFileObj.packageName)
{
return (int)(this.fileName - otherFileObj.fileName);
}
else
{
return (int)(otherFileObj.packageName -
this.packageName);
}
}


private string fileName;
private bool bVisited;
private string packageName;
}

I have trouble with the "CompareTo" method, because the operators '<'
or '-' can't be applied to string type. Anyone can advise me on how to
get this fixed?

I want to sort by packageName at first in the alphabetical order.
However, if two objects share a same packageName, then compare by
fileName, again in the alphabetical order.

Many thanks! This discussion forum has been so helpful to me.

-Emily

Mattias Sjögren
Guest
 
Posts: n/a
#2: Aug 23 '06

re: String Comparison in "CompareTo" Method


Emily,
Quote:
>I have trouble with the "CompareTo" method, because the operators '<'
>or '-' can't be applied to string type. Anyone can advise me on how to
>get this fixed?
Just delegate to String.CompareTo

public int CompareTo(object other)
{
FileName otherFileObj = (FileName)other;
int ret = this.packageName.CompareTo(otherFileObj.packageNam e);

if (ret == 0)
{
ret = this.fileName.CompareTo(otherFileObj.fileName);
}

return ret;
}


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Aug 23 '06

re: String Comparison in "CompareTo" Method


Mattias Sjögren <mattias.dont.want.spam@mvps.orgwrote:
Quote:
Emily,
Quote:
I have trouble with the "CompareTo" method, because the operators '<'
or '-' can't be applied to string type. Anyone can advise me on how to
get this fixed?
Just delegate to String.CompareTo

public int CompareTo(object other)
{
FileName otherFileObj = (FileName)other;
int ret = this.packageName.CompareTo(otherFileObj.packageNam e);

if (ret == 0)
{
ret = this.fileName.CompareTo(otherFileObj.fileName);
}

return ret;
}
Note that if you have quite a few comparisons to make, and you're using
..NET 2.0, you can improve readability using the null coalescing
operator:

http://msmvps.com/blogs/jon.skeet/ar...28/106119.aspx

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Fir5tSight
Guest
 
Posts: n/a
#4: Aug 23 '06

re: String Comparison in "CompareTo" Method


Hi Mattias

Awesome! This works!

-Emily

Closed Thread