Help me please -ArrayList.Sort() problems

  • Thread starter Thread starter jarkkotv
  • Start date Start date
J

jarkkotv

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.CurrentCulture;
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;

}
}
}
 
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.
 
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
 
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.
 
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 :)
 
Back
Top