F
Fir5tSight
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
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