473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

CompareTo issue.

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?
Jul 21 '05 #1
3 1326
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 Sp****@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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?

Jul 21 '05 #2
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 Sp****@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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?


Jul 21 '05 #3
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" <Be*******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
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 Sp****@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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?


Jul 21 '05 #4

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

Similar topics

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...
3
by: Ben Spigle | last post by:
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...
3
by: Fir5tSight | last post by:
Hi All, I have an interface class defined as follows: class FileName : IComparable { public FileName(string fileName, string packageName) { // // TODO: Add constructor logic here
2
by: tony | last post by:
Hello! I have a class lets call it Test with four fields. This Test class implement IComparable so CompareTo is therefore implemented in this Test class. One field in this Test class is an int...
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
3
by: Dhananjay | last post by:
Hi All, I am facing problem when i am converting C#.net code(Delegate concept) into vb.net. I am unable to do that . Can someone help me to solve the problem. I am providing my C#.net code. ...
2
by: Tony | last post by:
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...
2
by: Jon Skeet [C# MVP] | last post by:
Author <gnewsgroup@gmail.comwrote: Where? It looks to me like it's calling double's implementation of CompareTo. -- Jon Skeet - <skeet@pobox.com> Web site: http://www.pobox.com/~skeet Blog:...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.