Unexpected sorting results

Nov 12, 2008 at 4:32 PM

When sorting a DataGridTextColumn I get unexpected sorting results on strings containing Swedish characters (such as 'Å', 'Ä', 'Ö').
For example strings beginning with 'Å' gets placed among strings beginning with 'A' when sorting ascending, which is wrong since
"A".CompareTo("Å") == -1, but it seems like the datagrid sorting thinks that "A".CompareTo("Å") == 0.

Any ideas why I get these results?


Nov 13, 2008 at 1:49 PM
We use the same sorting technique that all ItemsControls use.  Do you still see the same result when you sort the same data in a ListView?
Nov 13, 2008 at 3:15 PM
Edited Nov 13, 2008 at 4:09 PM
Interesting... Indeed I do!

I've set up a WPF ListView containing three ListViewItems: "Åaa, Aaa, Aba", running the following:

listView1.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("Content", System.ComponentModel.ListSortDirection.Ascending));

renders the list in following order:


(When using good old trustworthy WinForms ListView the list is sorted correctly => Aaa, Aba, Åaa)

So... How is this solved in the most appropriate way?

Nov 13, 2008 at 5:28 PM
I'm not sure of the most appropriate way, but the most extensible is to create your own custom sort.  You can see an example of this here, http://blogs.msdn.com/jgoldb/archive/2008/08/26/improving-microsoft-datagrid-ctp-sorting-performance.aspx.
Nov 13, 2008 at 7:07 PM
I think there is some problem with the CollectionView's sorting implementation with these characters when the locale is en-US (usually the default).

Anyways, other possible solution could be to set the CollectionView's Culture to sv-SE (Swedish culture) which sorts appropriately (if setting the culture to sv-SE is ok for your app).

CollectionViewSource.GetDefaultView(DataGrid_Standard.ItemsSource).Culture = new CultureInfo(1053); // Setting the culture to sv-SE.


You could set it on the CollectionViewSource itself like...

CollectionViewSource cvs = new CollectionViewSource();
cvs.Source = <myItemsSource>;
vcs.Culture = new CultureInfo(1053);
DataGrid_Standard.ItemsSource = cvs.View;

Remember that this should be done after the ItemsSource is actually set on the DataGrid.
Nov 17, 2008 at 12:37 PM
Thanks for your advice guys. I simply used the xml:lang attribute:

 <dg:DataGrid xml:lang="sv-SE" />

That seems to work OK for now.