Home All Groups Group Topic Archive Search About

SortedList - bug or undocumented behavior ?

Author
8 Nov 2006 1:25 PM
Antonio Paglia
Hello. I have tried to insert this items into a SortedList.

dic = New SortedList
dic.Add("<<", "<<")
dic.Add("==", "==")
dic.Add(">>", ">>")
dic.Add("@@", "@@")
dic.Add("??", "??")

Debugging this peace of code I have notice that '??' appears as first item,
then '@@', etc...Ascii codes for '?' and '@' are greater than '<' '=' '>'.
May be this is a bug or an undocumented behavior ??

TIA
Antonio

Author
8 Nov 2006 2:09 PM
Mike McIntyre
You can implement the IComparable interface to control the sort order -> http://msdn2.microsoft.com/en-us/library/system.collections.sortedlist.aspx

I believe this code will give you the order you expect:

Private Sub DemoIComparer()

      Dim dic As New SortedList(New CustomComparer)



      dic.Add("<<", "<<")

      dic.Add("==", "==")

      dic.Add(">>", ">>")

      dic.Add("@@", "@@")

      dic.Add("??", "??")



      For Each key As DictionaryEntry In dic

          MessageBox.Show(key.ToString & "  " & key.Value.ToString)

      Next

  End Sub





  Public Class CustomComparer

      Implements IComparer



      Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare

          Dim s1 As String = CStr(x)

          Dim s2 As String = CStr(y)

          Return -[String].Compare(s1, s2)



      End Function

  End Class


--
Mike

Mike McIntyre [MVP]
http://www.getdotnetcode.com


Show quoteHide quote
"Antonio Paglia" <t***@algoritmo.com.ar> wrote in message news:evWhNlzAHHA.3380@TK2MSFTNGP04.phx.gbl...
>
> Hello. I have tried to insert this items into a SortedList.
>
> dic = New SortedList
> dic.Add("<<", "<<")
> dic.Add("==", "==")
> dic.Add(">>", ">>")
> dic.Add("@@", "@@")
> dic.Add("??", "??")
>
> Debugging this peace of code I have notice that '??' appears as first item,
> then '@@', etc...Ascii codes for '?' and '@' are greater than '<' '=' '>'.
> May be this is a bug or an undocumented behavior ??
>
> TIA
> Antonio
>
>
Author
8 Nov 2006 3:08 PM
Antonio Paglia
Thanks Mike and Chris for your information. This was very important for me !!

Mike, your implementation of IComparer works for that items but for these

        dic.Add("dd", "dd")
        dic.Add("aa", "aa")
        dic.Add("ab", "ab")
        dic.Add("==", "==")
        dic.Add(">>", ">>")
        dic.Add("@@", "@@")
        dic.Add("??", "??")
        dic.Add("HELLO", "HELLO")

don't works. I have change your implementation for this one :
Public Class CustomComparer
    Implements IComparer

    Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare

        Dim s1 As String = CStr(x)
        Dim s2 As String = CStr(y)

        Select Case True
            Case s1 < s2
                Return -1
            Case s1 = s2
                Return 0
            Case s1 > s2
                Return 1
        End Select

        Return 0
    End Function

End Class


The order was:

==
>>
??
@@
HELLO
aa
ab
dd


like I aspected. :-))

Thank you very much

Antonio








"Mike McIntyre" <mik***@getdotnetcode.com> escribió en el mensaje news:O$ufY%23zAHHA.4060@TK2MSFTNGP03.phx.gbl...
  You can implement the IComparable interface to control the sort order -> http://msdn2.microsoft.com/en-us/library/system.collections.sortedlist.aspx

  I believe this code will give you the order you expect:

  Private Sub DemoIComparer()

        Dim dic As New SortedList(New CustomComparer)



        dic.Add("<<", "<<")

        dic.Add("==", "==")

        dic.Add(">>", ">>")

        dic.Add("@@", "@@")

        dic.Add("??", "??")



        For Each key As DictionaryEntry In dic

            MessageBox.Show(key.ToString & "  " & key.Value.ToString)

        Next

    End Sub





    Public Class CustomComparer

        Implements IComparer



        Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare

            Dim s1 As String = CStr(x)

            Dim s2 As String = CStr(y)

            Return -[String].Compare(s1, s2)



        End Function

    End Class


  --
  Mike

  Mike McIntyre [MVP]
  http://www.getdotnetcode.com


Show quoteHide quote
  "Antonio Paglia" <t***@algoritmo.com.ar> wrote in message news:evWhNlzAHHA.3380@TK2MSFTNGP04.phx.gbl...
  >
  > Hello. I have tried to insert this items into a SortedList.
  >
  > dic = New SortedList
  > dic.Add("<<", "<<")
  > dic.Add("==", "==")
  > dic.Add(">>", ">>")
  > dic.Add("@@", "@@")
  > dic.Add("??", "??")
  >
  > Debugging this peace of code I have notice that '??' appears as first item,
  > then '@@', etc...Ascii codes for '?' and '@' are greater than '<' '=' '>'.
  > May be this is a bug or an undocumented behavior ??
  >
  > TIA
  > Antonio
  >
  >
Author
8 Nov 2006 3:23 PM
Mike McIntyre
Good job!

--
Mike

Mike McIntyre [MVP]
http://www.getdotnetcode.com


  "Antonio Paglia" <t***@algoritmo.com.ar> wrote in message news:eFemBf0AHHA.2140@TK2MSFTNGP02.phx.gbl...

  Thanks Mike and Chris for your information. This was very important for me !!

  Mike, your implementation of IComparer works for that items but for these

          dic.Add("dd", "dd")
          dic.Add("aa", "aa")
          dic.Add("ab", "ab")
          dic.Add("==", "==")
          dic.Add(">>", ">>")
          dic.Add("@@", "@@")
          dic.Add("??", "??")
          dic.Add("HELLO", "HELLO")

  don't works. I have change your implementation for this one :
  Public Class CustomComparer
      Implements IComparer

      Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare

          Dim s1 As String = CStr(x)
          Dim s2 As String = CStr(y)

          Select Case True
              Case s1 < s2
                  Return -1
              Case s1 = s2
                  Return 0
              Case s1 > s2
                  Return 1
          End Select

          Return 0
      End Function

  End Class


  The order was:

  ==
  >>
  ??
  @@
  HELLO
  aa
  ab
  dd


  like I aspected. :-))

  Thank you very much

  Antonio








  "Mike McIntyre" <mik***@getdotnetcode.com> escribió en el mensaje news:O$ufY%23zAHHA.4060@TK2MSFTNGP03.phx.gbl...
    You can implement the IComparable interface to control the sort order -> http://msdn2.microsoft.com/en-us/library/system.collections.sortedlist.aspx

    I believe this code will give you the order you expect:

    Private Sub DemoIComparer()

          Dim dic As New SortedList(New CustomComparer)



          dic.Add("<<", "<<")

          dic.Add("==", "==")

          dic.Add(">>", ">>")

          dic.Add("@@", "@@")

          dic.Add("??", "??")



          For Each key As DictionaryEntry In dic

              MessageBox.Show(key.ToString & "  " & key.Value.ToString)

          Next

      End Sub





      Public Class CustomComparer

          Implements IComparer



          Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare

              Dim s1 As String = CStr(x)

              Dim s2 As String = CStr(y)

              Return -[String].Compare(s1, s2)



          End Function

      End Class


    --
    Mike

    Mike McIntyre [MVP]
    http://www.getdotnetcode.com


Show quoteHide quote
    "Antonio Paglia" <t***@algoritmo.com.ar> wrote in message news:evWhNlzAHHA.3380@TK2MSFTNGP04.phx.gbl...
    >
    > Hello. I have tried to insert this items into a SortedList.
    >
    > dic = New SortedList
    > dic.Add("<<", "<<")
    > dic.Add("==", "==")
    > dic.Add(">>", ">>")
    > dic.Add("@@", "@@")
    > dic.Add("??", "??")
    >
    > Debugging this peace of code I have notice that '??' appears as first item,
    > then '@@', etc...Ascii codes for '?' and '@' are greater than '<' '=' '>'.
    > May be this is a bug or an undocumented behavior ??
    >
    > TIA
    > Antonio
    >
    >
Author
8 Nov 2006 2:17 PM
Chris Dunaway
Antonio Paglia wrote:
> Hello. I have tried to insert this items into a SortedList.
>
> dic = New SortedList
> dic.Add("<<", "<<")
> dic.Add("==", "==")
> dic.Add(">>", ">>")
> dic.Add("@@", "@@")
> dic.Add("??", "??")
>
> Debugging this peace of code I have notice that '??' appears as first item,
> then '@@', etc...Ascii codes for '?' and '@' are greater than '<' '=' '>'.
> May be this is a bug or an undocumented behavior ??

Your keys are strings so the sorting order is determined by the
IComparable implementation in the string class.  The sort may not be an
ASCII sort, depending on the CurrentCulture used.

>From the docs on the string class:

"Sort rules determine the alphabetic order of Unicode characters and
how two strings compare to each other. For example, the Compare method
performs a linguistic comparison while the CompareOrdinal method
performs an ordinal comparison. Consequently, if the current culture is
U.S. English, the Compare method considers 'a' less than 'A' while the
CompareOrdinal method considers 'a' greater than 'A'.

The .NET Framework supports word, string, and ordinal sort rules. A
word sort performs a culture-sensitive comparison of strings in which
certain nonalphanumeric Unicode characters might have special weights
assigned to them. For example, the hyphen ("-") might have a very small
weight assigned to it so that "coop" and "co-op" appear next to each
other in a sorted list. A string sort is similar to a word sort, except
that there are no special cases and all nonalphanumeric symbols come
before all alphanumeric Unicode characters. An ordinal sort compares
strings based on the numeric value of each Char in the string. For more
information about word, string, and ordinal sort rules, see the
System.Globalization.CompareOptions topic."
Author
8 Nov 2006 5:59 PM
Cor Ligthert [MVP]
Antonio,

The sorted list is a pair of objects, one is the Key and one is the Value.

The meaning from this is that you get an Value using the key, it is not a
kind of arraylist.

Therefore, access them using the value and not in a foreach loop, otherwise
an arraylist is good enough,

Just my opinion,

Cor

Show quoteHide quote
"Antonio Paglia" <t***@algoritmo.com.ar> schreef in bericht
news:evWhNlzAHHA.3380@TK2MSFTNGP04.phx.gbl...
>
> Hello. I have tried to insert this items into a SortedList.
>
> dic = New SortedList
> dic.Add("<<", "<<")
> dic.Add("==", "==")
> dic.Add(">>", ">>")
> dic.Add("@@", "@@")
> dic.Add("??", "??")
>
> Debugging this peace of code I have notice that '??' appears as first
> item, then '@@', etc...Ascii codes for '?' and '@' are greater than '<'
> '=' '>'. May be this is a bug or an undocumented behavior ??
>
> TIA
> Antonio
>
>
Author
11 Nov 2006 4:51 PM
Jay B. Harlow
Antonio,
In addition to the other comments:
> then '@@', etc...Ascii codes for '?' and '@' are greater than '<' '=' '>'.

Remember .NET stores strings in Unicode, specifically UTF-16. UTF-16 means
that the Unicode characters (which can be upto 32-bits) are stored as 16 bit
values.

This is an excellent article on the perils & pitfalls of comparing strings
in .NET 2.0:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/StringsinNET20.asp

By default strings are compared via the CurrentCulture. It sounds like you
are expecting the strings to be compared via ASCII Ordinal, not even Unicode
Ordinal.

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Antonio Paglia" <t***@algoritmo.com.ar> wrote in message
news:evWhNlzAHHA.3380@TK2MSFTNGP04.phx.gbl...
>
> Hello. I have tried to insert this items into a SortedList.
>
> dic = New SortedList
> dic.Add("<<", "<<")
> dic.Add("==", "==")
> dic.Add(">>", ">>")
> dic.Add("@@", "@@")
> dic.Add("??", "??")
>
> Debugging this peace of code I have notice that '??' appears as first
> item, then '@@', etc...Ascii codes for '?' and '@' are greater than '<'
> '=' '>'. May be this is a bug or an undocumented behavior ??
>
> TIA
> Antonio
>
>