471,320 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,320 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 1713
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by malcolm | last post: by
7 posts views Thread by tyler_durden | last post: by
7 posts views Thread by Stephen | last post: by
13 posts views Thread by Lloyd Sheen | last post: by
12 posts views Thread by adamurbas | last post: by
reply views Thread by rosydwin | last post: by

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.