1

When sorting a ListView with items starting with special characters (Åå/Ää/Öö) they don't end up where they should.

'Testing' should come before 'Ö:Last place!!'

Do I have to implement a custom sorting method or is there a better way?

If yes on the custom sort, how do I simply sort by ABC, including special characters?

Edit: This is my current sort method:

                <GridViewColumn DisplayMemberBinding="{Binding Name}" Width="150">
                    <GridViewColumn.Header>
                        <GridViewColumnHeader Tag="Name" Click="SortByColumns">Name</GridViewColumnHeader>
                    </GridViewColumn.Header>
                </GridViewColumn>

...

    private void SortByColumns(object sender, RoutedEventArgs e)
    {
        GridViewColumnHeader column = (sender as GridViewColumnHeader);
        string sortBy = column.Tag.ToString();
        if (listViewSortCol != null)
        {
            AdornerLayer.GetAdornerLayer(listViewSortCol).Remove(listViewSortAdorner);
            siteListView.Items.SortDescriptions.Clear();
        }

        ListSortDirection newDir = ListSortDirection.Ascending;
        if (listViewSortCol == column && listViewSortAdorner.Direction == newDir)
            newDir = ListSortDirection.Descending;

        listViewSortCol = column;
        listViewSortAdorner = new SortAdorner(listViewSortCol, newDir);
        AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);
        siteListView.Items.SortDescriptions.Add(new SortDescription(sortBy, newDir));
    }
Felix
  • 138
  • 2
  • 11

1 Answers1

1

You should just be able to set the Language property of the DataGrid to your language:

<DataGrid Language="sv" ...>

If you set or bind the ItemsSource of the ListView to an IList, you could use a custom IComparer to implement some completely custom sorting logic.

Please refer to the following sample code.

public class StringComparer<T> : IComparer where T : class
{
    private static readonly CultureInfo culture = new CultureInfo("sv-SE");
    private string _propertyName;
    private int _direction;


    public StringComparer(string propertyName, ListSortDirection direction)
    {
        _propertyName = propertyName;
        _direction = direction == ListSortDirection.Descending ? 1 : -1;
    }

    public int Compare(object x, object y)
    {
        if (x == null && y == null)
            return 0;
        else if (x == null)
            return -1;
        else if (y == null)
            return 1;
        else
        {
            PropertyInfo pi = typeof(T).GetProperty(_propertyName);
            if (pi == null)
                return 0;

            object a = pi.GetValue(x);
            object b = pi.GetValue(y);

            if (a == null && b == null)
                return 0;
            else if (a == null)
                return -1;
            else if (b == null)
                return 1;

            return string.Compare(a.ToString(), b.ToString(), false, culture) * _direction;
        }
    }
}

private void SortByColumns(object sender, RoutedEventArgs e)
{
    GridViewColumnHeader column = (sender as GridViewColumnHeader);
    string sortBy = column.Tag.ToString();
    if (listViewSortCol != null)
    {
        AdornerLayer.GetAdornerLayer(listViewSortCol).Remove(listViewSortAdorner);
        siteListView.Items.SortDescriptions.Clear();
    }

    ListSortDirection newDir = ListSortDirection.Ascending;
    if (listViewSortCol == column /*&& listViewSortAdorner.Direction == newDir*/)
        newDir = ListSortDirection.Descending;

    listViewSortCol = column;
    listViewSortAdorner = new SortAdorner(listViewSortCol, newDir);
    AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);

    ListCollectionView lvi = CollectionViewSource.GetDefaultView(siteListView.ItemsSource) as ListCollectionView;
    if (lvi != null)
    {
        lvi.CustomSort = new StringComparer<MyTuple>(sortBy, newDir);
    }
    else
        siteListView.Items.SortDescriptions.Add(new SortDescription(sortBy, newDir));

}
mm8
  • 163,881
  • 10
  • 57
  • 88