0

Is it a well known issue that SortDescription do not work correctly for certain Cultures, or are there any obvious setup that I am missing?

In my case I use nb-NO and get all lists in wrong order (ComboBoxes/ListBoxes) in a WPF application. However I get the correct order when populating an ObservableCollection with a pre-sorted query/List() using Linq to entities.

Anyone else who have experienced the same, and have a solution to this issue?

user2467020
  • 231
  • 2
  • 3
  • Is the sorting working if you don't do anything special with the nb-NO, suppose your binding is to the observablecollection in the first place your sortdescription on the collecitonviewsource will not help. I don't know because i don't see your code. – Philip Stuyck Feb 13 '15 at 17:24
  • The code is quite simple. GetEmployees return an ObservableCollection. The sorting works regarding english letters. Norwegian letters like Æ, Ø and Å is presented in wrong order. The binding is to Employees. Employees = GetEmployees(); ICollectionView employeeView = CollectionViewSource.GetDefaultView(Employees); employeeView.SortDescriptions.Add(new SortDescription("LastName", ListSortDirection.Ascending)); – user2467020 Feb 13 '15 at 18:43
  • Did you change the current culture to norwegian ? Otherwise you get unicode sorting. – Philip Stuyck Feb 13 '15 at 18:58
  • Anyway i think it is a good idea to check the value of the currentculture in your application. – Philip Stuyck Feb 13 '15 at 19:04
  • Thanks. The current culture is nb-NO. – user2467020 Feb 13 '15 at 19:47
  • Even if the reported current culture seems ok, I probably look more into this. As I use a Norwegian computer the culture seems to be set automatically. Maybe this is overridden somehow in the WPF-app. Thanks for suggestion. I understand it is quite difficult as I may not understand what kind of information is needed to support to find a solution.. – user2467020 Feb 13 '15 at 19:55
  • Check the value of this in your application : Thread.CurrentThread.CurrentCulture is it Norwegian ? – Philip Stuyck Feb 13 '15 at 20:12
  • Thread.CurrentThread.CultureInfo is nb-NO – user2467020 Feb 13 '15 at 20:27
  • 1
    Solution: By some reason the CollectionViewSource culture property was null. So by setting this property to Thread.CurrentThread.CurrentCulture everything works just fine. I expected this property automatically was set to the CurrentCultue, which was not the case. – user2467020 Feb 15 '15 at 07:30
  • Alternate solution: I had the same issue, but setting CollectionView.Culture did not help. I discovered that Control Panel->Change Region->Format had selected english. When I changed this to Norwegian, sorting worked as expected – bigfoot Oct 09 '18 at 11:29

2 Answers2

0

Are you preserving the order of the data set as it's returned from the underlying data source? If your data is coming from SQL Server, Oracle, etc. you might have the wrong culture on that side of the ocean.

Philip S.
  • 1
  • 1
0

As a workaround it seems to be possible to implement your own sorter.

From jsirr13's answer to a different question:

[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
    public static extern int StrCmpLogicalW(string psz1, string psz2);
}

public sealed class NaturalStringComparer : IComparer<string>
{
    public int Compare(object a, object b)
    {
        var lhs = (MultiItem)a;
        var rhs = (MultiItem)b;
        //APPLY ALGORITHM LOGIC HERE
        return SafeNativeMethods.StrCmpLogicalW(lhs.SiteName, rhs.SiteName);
    }
}

And here's how I use the above algorithm comparer:

    private void SortCol()
    {
        var dataView =
                      (ListCollectionView)CollectionViewSource.GetDefaultView(ListViewMultiSites.ItemsSource);
        dataView.CustomSort = new NaturalOrderComparer();
        dataView.Refresh();
    }
Community
  • 1
  • 1
Vegard Larsen
  • 12,827
  • 14
  • 59
  • 102