By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,640 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,640 IT Pros & Developers. It's quick & easy.

SortedList - bug or undocumented behavior ?

P: n/a

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
Nov 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
You can implement the IComparable interface to control the sort order -http://msdn2.microsoft.com/en-us/lib...ortedlist.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
"Antonio Paglia" <to**@algoritmo.com.arwrote in message news:ev**************@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

Nov 8 '06 #2

P: n/a
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."

Nov 8 '06 #3

P: n/a

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" <mi****@getdotnetcode.comescribió en el mensaje news:O$****************@TK2MSFTNGP03.phx.gbl...
You can implement the IComparable interface to control the sort order -http://msdn2.microsoft.com/en-us/lib...ortedlist.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
"Antonio Paglia" <to**@algoritmo.com.arwrote in message news:ev**************@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

Nov 8 '06 #4

P: n/a
Good job!

--
Mike

Mike McIntyre [MVP]
http://www.getdotnetcode.com
"Antonio Paglia" <to**@algoritmo.com.arwrote in message news:eF**************@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" <mi****@getdotnetcode.comescribió en el mensaje news:O$****************@TK2MSFTNGP03.phx.gbl...
You can implement the IComparable interface to control the sort order -http://msdn2.microsoft.com/en-us/lib...ortedlist.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
"Antonio Paglia" <to**@algoritmo.com.arwrote in message news:ev**************@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

Nov 8 '06 #5

P: n/a
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

"Antonio Paglia" <to**@algoritmo.com.arschreef in bericht
news:ev**************@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


Nov 8 '06 #6

P: n/a
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/de...ngsinNET20.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
"Antonio Paglia" <to**@algoritmo.com.arwrote in message
news:ev**************@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

Nov 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.