I have a list of my custom class Customer and I want to sort them alphabetically by Title. So I wrote
myList = myList.OrderByDescending(x => x.Title).ToList<Customer>();
Now the problem is that this method doesn't support the Swedish way of sorting the letters å, ä, ö. They should appear at the end after the letter z but they don't.
So I made a workaround method that replaces the Swedish letters before the ordering and then changes them back afterwords. It looks like this but it is quite slow. Can somebody think of a better way?
private List<Customer> OrderBySwedish(List<Customer> myList)
    {
        foreach (var customer in myList)
        {
            customer.Title = customer.Title.Replace("å", "zzz1").Replace("ä", "zzz2").Replace("ö", "zzz3").Replace("Å", "Zzz1").Replace("Ä", "Zzz2").Replace("Ö", "Zzz3");
        }
        myList= myList.OrderBy(x => x.Title).ToList<Customer>();
        foreach (var customer in myList)
        {
            customer.Title = customer.Title.Replace("zzz1", "å").Replace("zzz2", "ä").Replace("zzz3", "ö").Replace("Zzz1", "Å").Replace("Zzz2", "Ä").Replace("Zzz3", "Ö");
        }
        return myList;
    }
You can use culture specific StringComparer, see here.
CultureInfo culture = new CultureInfo("sv-SE");
var result = myList.OrderByDescending(x => 
               x.Title, StringComparer.Create(culture, false));
Set the Thread.CurrentCulture property to the correct culture.
I my case: _My sorting list have value was encoded. This make my order incorrect. Add decoded solving my problems !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With