Home > Software engineering >  In order By how to set character at first and symbol at last
In order By how to set character at first and symbol at last

Time:03-09

Below is my code

List<User> listOfUsers = new List<User>()
{
   new User() { Key = "AFG", Value = "Afghanistan" },
   new User() { Key = "ALA", Value = "Åland Islands"},
   new User() { Key = "BHR", Value = "Bahrain" },
   new User() { Key = "ARG", Value = "Argentina"},
};

List<User> usersByAge = listOfUsers.OrderBy(user => user.Value).ToList();
foreach (User user in usersByAge)
{
    var k = user.Key;
    var v = user.Value;
    Console.WriteLine(user.Key   ": "   user.Value);
 }


class User
{
public string Key { get; set; }
public string Value { get; set; }
}

I want Åland Islands to be display at last in the list instead of second as Åland Islands has symbol instead of A

As per my code it is displaying at 2nd but I want to Åland Islands to display at last

Can anyone help me in this?

CodePudding user response:

You should use StringComparer.Ordinal, that sorts according to the unicode codepoint.

Just replace with this:

List<User> usersByAge = listOfUsers.OrderBy(user => user.Value, StringComparer.Ordinal).ToList();

Now Åland will be last.

  • Related