473,396 Members | 1,995 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,396 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 1328
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.