Re: String.Compare seems to fail when it should not

Mark Hurd <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <[email protected]>
On Tue, Sep 9, 2008 at 5:57 AM, Sébastien Lorion
<[email protected]> wrote:
> My bottom line is, if I control the data and I am 100% sure it won't contain
> problematic chars, then by all means, I will use ordinal compare and get the
> speed increase. Otherwise, as a non English native myself, I want to make my
> apps international and so, I will use the culture aware comparison (and
> other operations).

Just be careful what you use the comparison for... Note this highly
rated Connect "Won't Fix":

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=236900

I bet this'd be a bugger of a bug to locate.

In this snippet you'd expect the two SortedLists to be identical, but
they're not, and the reason's clear -- the strings are s1<s2<s3<s1!

Dim s1 As String = "-0.67:-0.33:0.33"
Dim s2 As String = "0.67:-0.33:0.33"
Dim s3 As String = "-0.67:0.33:-0.33"
Console.WriteLine(s1.CompareTo(s2))
Console.WriteLine(s2.CompareTo(s3))
Console.WriteLine(s3.CompareTo(s1))
Dim l, m As New SortedList(Of String, Integer)
l.Add(S1,1)
l.Add(S2,2)
l.Add(S3,3)
m.Add(S3,3)
m.Add(S2,2)
m.Add(S1,1)
For Each i in m
 Console.WriteLine(i)
Next
For Each j in l
 Console.WriteLine(j)
Next

Now I know why FXCop and the built-in VS2008 Code Analysis always says
to specify the comparer and cultureinfo -- it's not just for performance!

Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.