473,473 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String Comparison in "CompareTo" Method

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

Aug 23 '06 #1
3 1551
Emily,
>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.
Aug 23 '06 #2
Mattias Sjögren <ma********************@mvps.orgwrote:
Emily,
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 - <sk***@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
Aug 23 '06 #3
Hi Mattias

Awesome! This works!

-Emily

Aug 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dheeraj | last post by:
Hi : I am writing a test utility where i wish to compare two Java objects. The object consists of many other objects. In other words the objects tree is deep. Does anyone one how to compare...
3
by: Gergely Varadi | last post by:
public class A : IComparable { int IComparable.CompareTo(object obj) { return 1; } } /// <summary> /// <see cref="A.System.IComparable.CompareTo"/>
5
by: majm | last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm using VS2005 beta 2. So far, System.Collections.Generic.List appears to be the ideal solution. However, the...
13
by: John A Grandy | last post by:
apparently references of type Date can not assume the value Nothing, because the following code fails: Dim d As Date .......other code...... If d Is Nothing Then End If so then what is...
1
by: Beje | last post by:
Greetings all, I am new to the java programming language. I seem to have hit the wall when I try to sort words a user inputs. I am trying to sort like four input words from the user and then...
5
by: forlist2001 | last post by:
Hello there, How do i compare string such that A < B<... <Y<Z<BA<BB... <CA... <ZZ Thanks KB
7
by: jpuopolo | last post by:
All: The String class has a CompareTo method, that compares to strings. Is there a similar method that performs case-insensitive comparisons? Thanks, John
5
by: Andrus | last post by:
I added reference to Microsoft.VisualBasic.dll to project. Line var r = Microsoft.VisualBasic.CompilerServices.CompareString("a", "b", true)<1; causes compile error: The type or...
2
by: lilyumestar | last post by:
This project is due by Tuesday and I haven't even gotten half of it done. Can anyone please help me with this Exception error? I've been trying to figure it out for several hours Error Message ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.