0

I am german fortunately. My treeview sorts like this:

A
B
C
...
Ä
Ö
....

It should sort like this:

A
Ä
B
C
...
O
Ö
....
...

How can I accomplish that?

Alex
  • 117
  • 11
  • 1
    Possible duplicate of [OrderBy with Swedish letters](https://stackoverflow.com/questions/7229445/orderby-with-swedish-letters) – Draken Sep 06 '19 at 15:03
  • 1
    Are you using `[TreeView].Sort()` to sort the Items? The sorting order in the *should sort* list is what you should actually get if the `Thread.CurrentThread.CurrentCulture` is Germal or English. It won't if the current culture is Swedish, for example (as in the possible duplicate). If you're using different locales, add this information to the question. – Jimi Sep 06 '19 at 15:14
  • Sort the data instead of the control. Just be careful with this, German collation rules are very quirky. For one, their dictionary isn't arranged the same way as their phonebook. The probable best choice is to make it sort the same way that any other program does, Explorer first. – Hans Passant Sep 06 '19 at 16:50

1 Answers1

0

I'm not sure how your treeview is sorting it's elements. However the following code could easily be modified to suit your particular treeview node sorting method.

You can sort a TreeView using an IComparer, you just need to set the TreeViewNodeSorterProperty.

var letters = new List<string> { "A", "B", "C", "O", "Ä", "Ö" };
IComparer<string> comparer = new StringComparer();
var orderedLetters = letters.OrderBy(x => comparer); // returns "A", "Ä", "B", "C", "O", "Ö"

public class StringComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {

        var comparisonResult = string.Compare(x, y, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace);
        var areSameUnderlyingLetters = comparisonResult == 0;

        if (areSameUnderlyingLetters)
        {
            return string.Compare(x, y);
        }

        return comparisonResult;
    }
}
openshac
  • 4,966
  • 5
  • 46
  • 77