Click here to Skip to main content
11,644,286 members (72,240 online)
Click here to Skip to main content

A Scary Bug in String.Compare, ArrayList.Sort, SortedList etc

, 13 Nov 2006 22.2K 6
Rate this:
Please Sign up or sign in to vote.
A Scary Bug in String.Compare, ArrayList.Sort, SortedList etc


I found this scary bug working with the SortedList. Certain keys I added could not be retrieved again. Investigation proved the problem extended to ArrayList.Sort and then to String.Compare.

It is possible to create three strings s1, s2, s3 so that String.Compare reports s1 > s2 and s2 > s3 and s3 > s1. Ie the string operation is not transitive.

This bug occurs, I believe, because of special logic in the string compare function to sort words like "coop" and co-op" together. However when two hyphens are used in a string the logic can fail. If you use two hyphens compare with String.CompareOrdinal until this problem is fixed.

Many thanks to Marc Gravell and others who helped identify the problem.

This problem has been reported to Microsoft at the link below. Please feel free to vote for it!


// A c# Console Application 
using System; 
class Class1
    static void Main(string[] args)
        string s1,s2,s3;

        s1 = "-0.67:-0.33:0.33"; 
        s2 = "0.67:-0.33:0.33"; 
        s3 = "-0.67:0.33:-0.33"; 
        Console.WriteLine( "String.Compare:" );
        Console.WriteLine( CompareTwoStrings(s1,s2,0) );
        Console.WriteLine( CompareTwoStrings(s2,s3,0) );
        Console.WriteLine( CompareTwoStrings(s3,s1,0) ); 
        Console.WriteLine( "CompareOrdinal:" );
        Console.WriteLine( CompareTwoStrings(s1,s2,1) );
        Console.WriteLine( CompareTwoStrings(s2,s3,1) );
        Console.WriteLine(CompareTwoStrings(s3,s1,1) ); 
    static string CompareTwoStrings( string s1, string s2, int mode)
        int i;
        if (mode==0)
        else if (mode==1)
            return null;
        if (i<0) return s1 + " is less than " + s2;
        if (i>0) return s1 + " is greater than " + s2;
        return s1 + " is equal to " + s2;


-0.67Blush | :O .33Blush | :O .33 is greater than 0.67Blush | :O .33Blush | :O .33
0.67Blush | :O .33Blush | :O .33 is greater than -0.67Blush | :O .33Blush | :O .33
-0.67Blush | :O .33Blush | :O .33 is greater than -0.67Blush | :O .33Blush | :O .33

-0.67Blush | :O .33Blush | :O .33 is less than 0.67Blush | :O .33Blush | :O .33
0.67Blush | :O .33Blush | :O .33 is greater than -0.67Blush | :O .33Blush | :O .33
-0.67Blush | :O .33Blush | :O .33 is greater than -0.67Blush | :O .33Blush | :O .33


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Win Myan14-Apr-09 6:21
memberWin Myan14-Apr-09 6:21 
GeneralNot a bug Pin
Mihai Nita13-Nov-06 9:17
memberMihai Nita13-Nov-06 9:17 
GeneralYes it is! Pin
williammorrishooper13-Nov-06 20:44
memberwilliammorrishooper13-Nov-06 20:44 
GeneralIt is made by design Pin
oupoi13-Nov-06 20:58
memberoupoi13-Nov-06 20:58 
GeneralI don't think you work for microsoft Pin
williammorrishooper13-Nov-06 23:22
memberwilliammorrishooper13-Nov-06 23:22 
GeneralRe: I don't think you work for microsoft Pin
Mihai Nita14-Nov-06 19:49
memberMihai Nita14-Nov-06 19:49 
GeneralRe: Yes it is! Pin
Mihai Nita14-Nov-06 19:55
memberMihai Nita14-Nov-06 19:55 
Generalnutter Pin
williammorrishooper17-Nov-06 0:25
memberwilliammorrishooper17-Nov-06 0:25 
GeneralRe: nutter [modified] Pin
Mihai Nita17-Nov-06 6:09
memberMihai Nita17-Nov-06 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 13 Nov 2006
Article Copyright 2006 by w.hooper
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid