Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2005

Tip: Looking for answers? Try searching our database.

SortedList order broken when keys contain "-"?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff Mason - 14 Sep 2005 17:17 GMT
Hi,

I think the SortedList does not order itself correctly (or at least
consistently) when its keys contain certain special characters, e.g. the "-"
(hyphen) character.  I find this a little hard to believe, so I assume it's
something I don't understand.

Consider the VB.NET console application code below:
-------
Module Module1
  Sub Main()

     Dim s As New SortedList
     s.Add("R", "valR")
     s.Add("X", "valX")
     s.Add("RA", "valRA")
     s.Add("R+", "valR+")
     s.Add("R+A", "valR+A")
     s.Add("R+B", "valR+B")
     s.Add("RB", "valRB")
     s.Add("R+X", "valR+X")
     s.Add("RX", "valRY")

     For i As Integer = 0 To s.Count - 1
        Console.WriteLine(s.GetKey(i))
     Next
     Console.ReadLine()

  End Sub
------------

Running this will produce output in (what I think is) the correct order.
That is, the value "R" comes first, then all of the keys with values starting
with "R+" come next, then all strings with other characters following the "R"
come next in alphabetical order. The order I get is:

R
R+
R+A
R+B
R+X
RA
RB
RX
X

Which I think is correct.  Now, if I simply replace all the "+" characters
in the code above with a "-", the collection is no longer in that same order.
Instead I get:

R
R-
RA
R-A
RB
R-B
RX
R-X
X

It seems that this behavior occurs only with the "-"; I've tried several
other special characters and they seem to give the correct results.

But, if I replace with a "~", which I thought sorts after alphabetic
characters, I get an order like using the "+" character; that is, all the
"R~" strings come before the other R strings.  I think a string starting with
"R~" should sort after "RA", at least according to the character map.

I get the same results if I create the sorted list using
CaseInsensitveComparer in the constructor.

What don't I understand?

Thanks for any insight anyone can give.
Filipe Marcelino - 14 Sep 2005 17:42 GMT
Hi,

I'm not sure but I think that's an errorbecause, maybe the algorithm of
sort is confusing the char '-' (ascii value = 45) and char '-' (196)
witch, graphicaly it's the same...

Regards,
Filipe Marcelino
William Stacey [MVP] - 14 Sep 2005 17:53 GMT
I think if you use StringComparer.OrdinalIgnoreCase like below, you should
get the results you expect:

SortedList<string, string> s = new SortedList<string,
string>(StringComparer.OrdinalIgnoreCase);

Signature

William Stacey [MVP]

> Hi,
>
[quoted text clipped - 76 lines]
>
> Thanks for any insight anyone can give.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.