473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

SortedList - bug or undocumented behavior ?


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
6 1525
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gerrod | last post by:
Hi - Does anyone know a way to created a SortedList (in the System.Collections namespace) that will sort on VALUES instead of KEYS... ? The scenario is this - I have a SortedList containing...
6
by: Jose Jarabo | last post by:
Hello, thanks in advance for any and all replies. I am not sure how to categorize this, either a bug or something else but here is my problem. I am testing with 2 elements on a sorted list. If...
2
by: Pekka | last post by:
Could somebody say why the piece of code below does not work? My purpose is to renumber keys in a SortedList (after removal of an item) so that the keys would always contain an unbroken sequence of...
2
by: KrippZ | last post by:
Hello I'm having some problems with a a sortedList in C#. My set is as follows: I have a sortedList as a class variable (DataList) I inisiate it in my constructor with DataList = new SortedList()....
2
by: Prez | last post by:
I started writing .net code yesterday and I am grasping it well enough. I have a few questions about SortedLists. I am using managed C++ if that makes any difference. Of the examples I...
4
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>,...
4
by: aeshiels | last post by:
Hello, I have a SortedList defined as... SortedList<CUser, CUseruserList = new SortedList<CUser, CUser>(); ....and which to copy it to another sorted list SortedList<CUser,...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
6
by: n3tx | last post by:
Hi! I have a problem with sortedlist, i guess i dont understand how it works. I have a method called GetPublishingPlaces that returns an IList<PublishingPlace> (ex. contains 11 rows) I want...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.