473,396 Members | 2,011 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,396 software developers and data experts.

Strange String Sorting

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

Mar 1 '07 #1
2 2464
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.

"escher4096" <ca*****@ogmios.cawrote in message
news:11*********************@s48g2000cws.googlegro ups.com...
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

Mar 1 '07 #2
escher4096 wrote:
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.
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

--
Göran Andersson
_____
http://www.guffa.com
Mar 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: shyner | last post by:
Hi Everyone, I've been battling this for two days with no luck. I'm using SQL Server 2000. Here's the mystery: I've got a stored procedure that takes a single varchar parameter to determine...
1
by: Kepler | last post by:
I'm fighting a really strange bug that involves both a DataGrid and a Repeater disappearing on postback. The strange thing is that I've distilled the problem down to a simple program, that...
4
by: ^MisterJingo^ | last post by:
I am getting the following error: Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error...
10
by: Dustan | last post by:
I'm hiding some of the details here, because I don't want to say what I'm actually doing. I have a special-purpose class with a __cmp__ method all set up and ready to go for sorting. Then I have...
20
by: SpreadTooThin | last post by:
I have a list and I need to do a custom sort on it... for example: a = #Although not necessarily in order def cmp(i,j): #to be defined in this thread. a.sort(cmp) print a
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
7
by: abracadabra | last post by:
I am reading an old book - Programming Pearls 2nd edition recently. It says, "Even though the general C++ program uses 50 times the memory and CPU time of the specialized C program, it requires...
7
by: christery | last post by:
Anyone got a clue to why ther is a T between date and time in the "formatted for sorting" or whatewer they call it, and a Z at the end after seconds- got it fixed for my cobol programmer by...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.