Strange String Sorting

E

escher4096

Hi all,

In C# 2.0, if I take a set of strings and toss them into an ArrayList
and then sort them it does not always work as expected.

I used some garbage data (I just grabbed the using statements from the
top of my own program which were out of order):

using System;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

sorted them and got:

using System.Collections.Specialized;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System;

when I expected:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

I played around with it a little bit and found out that when I removed
the ';' they sorted as expected. Put the ';' back in and whackyness
ensues. This just seems odd to me, is there a special IComparer class
I should be using instead of the default string one? Does anyone know
why the default string sort wouldn't have sorted this?

Thanks

-Cam
 
M

Michael A. Covington

The sorting is correct. Look at the ASCII codes for period and semicolon.
Unicode is the same.

I suppose you could write your own comparer if you want to treat '.' and ';'
as equal.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

escher4096 said:
Hi all,

In C# 2.0, if I take a set of strings and toss them into an ArrayList
and then sort them it does not always work as expected.

I used some garbage data (I just grabbed the using statements from the
top of my own program which were out of order):

using System;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

sorted them and got:

using System.Collections.Specialized;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System;

when I expected:

There is the error. You expect something that is not correct.

The character code for period is lower than the character code of
semicolon, that's why periods are sorted to come before semicolon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Using statements inside namespace 5
Placement of Using 3
getting rid of the carot 10
Pass values b/w forms using properties 4
Screen Color 12
Print question 14
Form1.Visible= false C# 3
invalid report file path 0

Top