By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Help me please -ArrayList.Sort() problems

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.