473,382 Members | 1,400 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,382 software developers and data experts.

Array.Sort not sorting by character code value?

Why does the code below output this:
"v a" (118,32,97)
"w a" (119,32,97)
"v b" (118,32,98)
"w b" (119,32,98)
"v c" (118,32,99)
"w c" (119,32,99)

and not this (as one would expect if Array.Sort sorted by character
code value):
"v a" (118,32,97)
"v b" (118,32,98)
"v c" (118,32,99)
"w a" (119,32,97)
"w b" (119,32,98)
"w c" (119,32,99)
Is there any way to make Array.Sort sort strings by character code
value?

---------------------------

string[] strings = new string[] {
"w b",
"v a",
"v c",
"w a",
"v b",
"w c"
};

Array.Sort(strings);
PrintStrings(strings);

private void PrintStrings(string[] strings)
{
for (int i=0; i<strings.Length; i++)
{
Console.WriteLine(String.Format(" \"{0}\" ({1})",
strings[i],
GetASCIICharCodes(strings[i])));
}
}

private string GetASCIICharCodes(string s)
{
StringBuilder sb = new StringBuilder();

for (int i=0; i<s.Length; i++)
{
char c = Convert.ToChar(s.Substring(i, 1));

sb.Append(String.Format("{0}{1}",
((int)c).ToString(),
i<s.Length-1 ? "," : ""));
}

return sb.ToString();
}

Thanks,
Niklas Uhlin

Nov 17 '05 #1
3 2722
Hello,
Array.Sort(myArray) method uses default comparer to compare objects. It
gets comparer as Comparer.Default which is a comparer associated with
the Thread.CurrentCulture of the current thread. So it depends on the
current culture how does it behave in this scenario.
I have run your code and it displayed the strings in your desired
format.

Cheers :)
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2
Hi Niklas,

As Maqsood said, it is probably caused by your current culture.
As an example, for me, trying to sort the strings

"aaa"
"bbb"
"ccc"

I end up with

"bbb"
"ccc"
"aaa"

because 'aa' in a surname is pronounced as 'å' the last character in the Norwegian alphabet.

Hopefully we will be able to turn off or at least change this behaviour, without having to change culture, in Windows Longhorn.
Similarly you may encountere 'smart sorting' especially for numbers where
"12"
"1A"
is sorted as
"1A"
"12"

because Microsoft has decided that humans in general expect it that way and does not see numeric characters as characters, but numbers and completely different from other characters.

http://blogs.msdn.com/michkap/archiv...05/346933.aspx

At least you can turn off this behaviour using TweakUI

http://blogs.msdn.com/michkap/archiv...01/404830.aspx

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #3
Array.Sort(strings, new Comparer(new
System.Globalization.CultureInfo("sv-SE"))); // Swedish
.... produces the same error.
This is wierd because V comes before W in the swedish alphabet, so one
would think that sorting with a swedish CultureInfo would produce a
correct result.

If I on the other hand use
Array.Sort(myStringArray, new
Comparer(System.Globalization.CultureInfo.Invarian tCulture));
.... it works.

Thanks alot for the help!

Brgds,
Niklas Uhlin

Nov 17 '05 #4

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

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
1
by: LRW | last post by:
I have a page that's doing a search of a database, create an array and displays it. And I have it displaying the array just fine, but when I try to sort it, I get the error: Warning: sort()...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
4
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
9
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on....
16
by: randallc | last post by:
Hi, I need to take an input of 1D array, each item pipe delimited, and make a 2D array for column sorting.. Can anyone please help as my first ady with jscript, and haven't a clue why i get this...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
1
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.