473,396 Members | 1,917 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.

Help me please -ArrayList.Sort() problems

Hi everyone!

I'm having a little problem when sorting the ArrayList and I was
wondering if there is a .NET guru who can help me out :)

I'm trying to sort ArrayList alphabetically in ASP.Net -page using the
ArrayList.Sort(), but I not getting the results sorted properly.

I'm having problems especially with the scandic characters although
CurrentCulture is set to Finnish, which should handle these charcters
properly.

Thanks :)

Jarkko

Here is the code for the comparision I'm using currently...

internal class MyComparer : IComparer
{
public int Compare(object x, object y)
{
CultureInfo originalCulture =
System.Threading.Thread.CurrentThread.CurrentCultu re;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
try
{
String sx = x as String;
String sy = y as String;
if (sx != null && sy != null)
return String.Compare(sx, sy);
else
return Comparer.Default.Compare(x,y);
}
finally
{
Thread.CurrentThread.CurrentCulture = originalCulture;

}
}
}

Jul 29 '05 #1
4 1806
Hi Jarkko,

What result do you get, and what result did you expect? I see you are using InvariantCulture in your sorting, so any Finnish specific sorting rules are ignored. Using InvariantCulture will sort the strings as if they were English.
On Fri, 29 Jul 2005 09:48:48 +0200, jarkkotv <ja******@hotmail.com> wrote:
Hi everyone!

I'm having a little problem when sorting the ArrayList and I was
wondering if there is a .NET guru who can help me out :)

I'm trying to sort ArrayList alphabetically in ASP.Net -page using the
ArrayList.Sort(), but I not getting the results sorted properly.

I'm having problems especially with the scandic characters although
CurrentCulture is set to Finnish, which should handle these charcters
properly.

Thanks :)

Jarkko

Here is the code for the comparision I'm using currently...

internal class MyComparer : IComparer
{
public int Compare(object x, object y)
{
CultureInfo originalCulture =
System.Threading.Thread.CurrentThread.CurrentCultu re;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
try
{
String sx = x as String;
String sy = y as String;
if (sx != null && sy != null)
return String.Compare(sx, sy);
else
return Comparer.Default.Compare(x,y);
}
finally
{
Thread.CurrentThread.CurrentCulture = originalCulture;
}
}
}


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 29 '05 #2
Hi Morten,

Ok, thanks for the advice. I'll remove that part of the code and try if
it helps :)

I populate the ArrayList with the text coming from the MCMS. For
example, when ArrayList contains following values: A, B, C......
Result will look like the following after the the sort operation: A,
, B, C... as it should be A, B, C......

BR,

Jarkko

Jul 29 '05 #3
Yes, it is sorted by English sorting rules. Note that you don't need your own IComparer class to accomplish this. Setting CultureInfo before sorting would do the same.

string[] s = new string[]{"A",B",C",""};
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
ArrayList list = new ArrayList(s);

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
list.Sort();
Thread.CurrentThread.CurrentCulture = ci;

You could also use

Thread.CurrentThread.CurrentCulture = CultureInfo.GetSpecifiCulture("en-GB");

If you are globalizing your application and want to sort according to Finnish rules, you may be better off specifying a Finnish culture (fi-FI) before sorting.
On Fri, 29 Jul 2005 11:14:54 +0200, jarkkotv <ja******@hotmail.com> wrote:
Hi Morten,

Ok, thanks for the advice. I'll remove that part of the code and try if
it helps :)

I populate the ArrayList with the text coming from the MCMS. For
example, when ArrayList contains following values: A, B, C......
Result will look like the following after the the sort operation: A,
, B, C... as it should be A, B, C......

BR,

Jarkko


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 29 '05 #4
Thanks Morten, you are a lifesaver!

I tried your code snippet and it worked out fine after I used

Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("fi-FI") before the sort. I'll just
modify this a little bit to avoid such hard coding.

Jarkko :)

Jul 29 '05 #5

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

Similar topics

1
by: malcolm | last post by:
Hello, We have a small team building a project that involves some 30 or so c# assembly dlls. It is a client server application with 1 exe as the starting point. The dlls and exe are sharing an...
1
by: Dave | last post by:
I am having problems accessing DTS after install SP4 and was wondering if someone could offer some advice. I installed SP4 and got the following error after it competed. Unable to write to...
0
by: Rajesh Jain | last post by:
I Have 2 separate schemas. --------------Schema 1 is defined as below----------- <xs:schema targetNamespace="http://Schemas/1" xmlns="http://Schemas/1" xmlns:xs="http://www.w3.org/2001/XMLSchema"...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
4
by: Corey Dyke | last post by:
K here's what happened. For my English project at DeVry, I had to design a website for a company of my choice. So since my dad has been after me for years to do one for him, I decided to finally...
7
by: Stephen | last post by:
I have some code which I call from a custom validator however I seem to have got the logic wrong and im having trouble figuring out how to write my code to get things to work the way I require....
13
by: Lloyd Sheen | last post by:
I have now spent 5 hours on google/msdn looking for something useful in the creation of user controls for asp.net. The VS 2003 has very limited support for things such as absolute positioning of...
13
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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
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...

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.