Array.Sort not sorting by character code value?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Niklas Uhlin

    Array.Sort not sorting by character code value?

    Why does the code below output this:
    "v a" (118,32,97)
    "w a" (119,32,97)
    "v b" (118,32,98)
    "w b" (119,32,98)
    "v c" (118,32,99)
    "w c" (119,32,99)

    and not this (as one would expect if Array.Sort sorted by character
    code value):
    "v a" (118,32,97)
    "v b" (118,32,98)
    "v c" (118,32,99)
    "w a" (119,32,97)
    "w b" (119,32,98)
    "w c" (119,32,99)


    Is there any way to make Array.Sort sort strings by character code
    value?

    ---------------------------

    string[] strings = new string[] {
    "w b",
    "v a",
    "v c",
    "w a",
    "v b",
    "w c"
    };

    Array.Sort(stri ngs);
    PrintStrings(st rings);

    private void PrintStrings(st ring[] strings)
    {
    for (int i=0; i<strings.Lengt h; i++)
    {
    Console.WriteLi ne(String.Forma t(" \"{0}\" ({1})",
    strings[i],
    GetASCIICharCod es(strings[i])));
    }
    }

    private string GetASCIICharCod es(string s)
    {
    StringBuilder sb = new StringBuilder() ;

    for (int i=0; i<s.Length; i++)
    {
    char c = Convert.ToChar( s.Substring(i, 1));

    sb.Append(Strin g.Format("{0}{1 }",
    ((int)c).ToStri ng(),
    i<s.Length-1 ? "," : ""));
    }

    return sb.ToString();
    }

    Thanks,
    Niklas Uhlin

  • Maqsood Ahmed

    #2
    Re: Array.Sort not sorting by character code value?

    Hello,
    Array.Sort(myAr ray) method uses default comparer to compare objects. It
    gets comparer as Comparer.Defaul t which is a comparer associated with
    the Thread.CurrentC ulture of the current thread. So it depends on the
    current culture how does it behave in this scenario.
    I have run your code and it displayed the strings in your desired
    format.

    Cheers :)
    Maqsood Ahmed [MCP,C#]
    Kolachi Advanced Technologies


    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • Morten Wennevik

      #3
      Re: Array.Sort not sorting by character code value?

      Hi Niklas,

      As Maqsood said, it is probably caused by your current culture.
      As an example, for me, trying to sort the strings

      "aaa"
      "bbb"
      "ccc"

      I end up with

      "bbb"
      "ccc"
      "aaa"

      because 'aa' in a surname is pronounced as 'å' the last character in the Norwegian alphabet.

      Hopefully we will be able to turn off or at least change this behaviour, without having to change culture, in Windows Longhorn.


      Similarly you may encountere 'smart sorting' especially for numbers where
      "12"
      "1A"
      is sorted as
      "1A"
      "12"

      because Microsoft has decided that humans in general expect it that way and does not see numeric characters as characters, but numbers and completely different from other characters.



      At least you can turn off this behaviour using TweakUI





      --
      Happy coding!
      Morten Wennevik [C# MVP]

      Comment

      • Niklas Uhlin

        #4
        Re: Array.Sort not sorting by character code value?

        Array.Sort(stri ngs, new Comparer(new
        System.Globaliz ation.CultureIn fo("sv-SE"))); // Swedish
        .... produces the same error.
        This is wierd because V comes before W in the swedish alphabet, so one
        would think that sorting with a swedish CultureInfo would produce a
        correct result.

        If I on the other hand use
        Array.Sort(mySt ringArray, new
        Comparer(System .Globalization. CultureInfo.Inv ariantCulture)) ;
        .... it works.

        Thanks alot for the help!

        Brgds,
        Niklas Uhlin

        Comment

        Working...