PC Review


Reply
Thread Tools Rate Thread

CompareTo issue.

 
 
=?Utf-8?B?QmVuIFNwaWdsZQ==?=
Guest
Posts: n/a
 
      6th Aug 2004
I have a situtation where CompareTo is returning strange results when having
dashes in strings. The most concise illustration is:
"10050".CompareTo("1005-") = 1
"10050".CompareTo("1005-0") = -1
I have run alot more tests (I can post a short program if useful), but the
rule seems to be - < 0 at the end of a string but - = 0 in the middle of a
string.

Does anybody know what's going on here?
 
Reply With Quote
 
 
 
 
Sijin Joseph
Guest
Posts: n/a
 
      8th Aug 2004
When comparing strings less than and greater than are very hard to define,
the important thing we need to look at is equality.

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


"Ben Spigle" <Ben (E-Mail Removed)> wrote in message
news:4DDDFC4C-2B91-4A83-9FD4-(E-Mail Removed)...
> I have a situtation where CompareTo is returning strange results when

having
> dashes in strings. The most concise illustration is:
> "10050".CompareTo("1005-") = 1
> "10050".CompareTo("1005-0") = -1
> I have run alot more tests (I can post a short program if useful), but the
> rule seems to be - < 0 at the end of a string but - = 0 in the middle of a
> string.
>
> Does anybody know what's going on here?



 
Reply With Quote
 
=?Utf-8?B?QmVuIFNwaWdsZQ==?=
Guest
Posts: n/a
 
      8th Aug 2004
Hmm... Maybe you can explain what's so hard about sorting strings. In 25
years in computing I have never before encountered a system that couldn't
sort a list of strings. It's a pretty basic requirement in computing.

I'm trying to sort a list to use in a binary search, where the search key
does not have to be completely specified. Here's how the list I gave it
sorted (quotation marks are just to mark the beginning/end of the key.)
"1005-","10050","1005-0","100500","1005-00","1005-00
","100500 ","100501","1005-01",
"1005-01 ","100501 ","1005-1"
Given a key of "10050" you would hope to get "10050","100500","100500
". With this screwed up sort, however, the algorithm would terminate when
it it saw "1005-0" so neither "100500" and "100500 " would appear in
the result list.
"Sijin Joseph" wrote:

> When comparing strings less than and greater than are very hard to define,
> the important thing we need to look at is equality.
>
> --
> Sijin Joseph
> http://www.indiangeek.net
> http://weblogs.asp.net/sjoseph
>
>
> "Ben Spigle" <Ben (E-Mail Removed)> wrote in message
> news:4DDDFC4C-2B91-4A83-9FD4-(E-Mail Removed)...
> > I have a situtation where CompareTo is returning strange results when

> having
> > dashes in strings. The most concise illustration is:
> > "10050".CompareTo("1005-") = 1
> > "10050".CompareTo("1005-0") = -1
> > I have run alot more tests (I can post a short program if useful), but the
> > rule seems to be - < 0 at the end of a string but - = 0 in the middle of a
> > string.
> >
> > Does anybody know what's going on here?

>
>
>

 
Reply With Quote
 
Sijin Joseph
Guest
Posts: n/a
 
      9th Aug 2004
You're right, actually i was not thinking from that perspective. Anyways i
dug through the BCL code and then through Rotor, i have finally found the
code that String class in .Net uses for string comparison. It is is
\Rotor\sscli\clr\src\classlibnative\nls\comnlsinfo.cpp , go through the code
in this method, it sheds a lot of light on the workings of string
comparison. I haven't had the time to go through the code in detail, but
from what i see there is significant algorithm variance based on case
sensitivity, are you using a case sensitive search?


--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


"Ben Spigle" <(E-Mail Removed)> wrote in message
news:BA209C32-FBC5-42D4-B85C-(E-Mail Removed)...
> Hmm... Maybe you can explain what's so hard about sorting strings. In 25
> years in computing I have never before encountered a system that couldn't
> sort a list of strings. It's a pretty basic requirement in computing.
>
> I'm trying to sort a list to use in a binary search, where the search key
> does not have to be completely specified. Here's how the list I gave it
> sorted (quotation marks are just to mark the beginning/end of the key.)
> "1005-","10050","1005-0","100500","1005-00","1005-00
> ","100500 ","100501","1005-01",
> "1005-01 ","100501 ","1005-1"
> Given a key of "10050" you would hope to get "10050","100500","100500
> ". With this screwed up sort, however, the algorithm would terminate

when
> it it saw "1005-0" so neither "100500" and "100500 " would appear

in
> the result list.
> "Sijin Joseph" wrote:
>
> > When comparing strings less than and greater than are very hard to

define,
> > the important thing we need to look at is equality.
> >
> > --
> > Sijin Joseph
> > http://www.indiangeek.net
> > http://weblogs.asp.net/sjoseph
> >
> >
> > "Ben Spigle" <Ben (E-Mail Removed)> wrote in message
> > news:4DDDFC4C-2B91-4A83-9FD4-(E-Mail Removed)...
> > > I have a situtation where CompareTo is returning strange results when

> > having
> > > dashes in strings. The most concise illustration is:
> > > "10050".CompareTo("1005-") = 1
> > > "10050".CompareTo("1005-0") = -1
> > > I have run alot more tests (I can post a short program if useful), but

the
> > > rule seems to be - < 0 at the end of a string but - = 0 in the middle

of a
> > > string.
> > >
> > > Does anybody know what's going on here?

> >
> >
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with using CompareTo when having string Tony Microsoft C# .NET 1 28th May 2008 12:26 PM
Non-Transitive String.CompareTo Marc Gravell Microsoft Dot NET Framework 0 13th Nov 2006 01:15 PM
How do I use CompareTo in my case tony Microsoft C# .NET 2 31st Aug 2006 08:50 AM
Argument Exception with Double.CompareTo Brian Richards Microsoft C# .NET 1 12th Jun 2006 11:06 PM
String.Compareto Strangeness Gerry Wheeler Microsoft Dot NET 2 8th Oct 2005 01:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:44 PM.