Sort list in C#

Here are some examples of working with sorting lists in C#:

Example 1: Sorting a List in C#

List<int> numbers = new List<int> { 3, 1, 4, 1, 5, 9, 2, 6, 5 };

numbers.Sort();

foreach (var number in numbers) {
    Console.WriteLine(number);
}

In this example, a List<int> is initialized with several integer values. The Sort method is used to sort the list in ascending order, and then the resulting sorted list is printed to the console using a foreach loop.

Example 2: Sorting a List by Order A-B-Z then AA-BB-ZZ in C#

List<string> words = new List<string> { "BB", "Z", "aa", "z", "A", "b", "Zz", "a", "Bb" };

var sortedWords = words.OrderBy(word => word, StringComparer.OrdinalIgnoreCase);

foreach (var word in sortedWords) {
    Console.WriteLine(word);
}

In this example, a List<string> is initialized with several string values. The OrderBy method is used to sort the list by the string values in ascending order, while ignoring the case of the strings, using the StringComparer.OrdinalIgnoreCase comparer. The resulting sorted list is then printed to the console using a foreach loop.

Example 3: Using LINQ to Sort a List in C#

List<int> numbers = new List<int> { 3, 1, 4, 1, 5, 9, 2, 6, 5 };

var sortedNumbers = numbers.OrderBy(x => x);

foreach (var number in sortedNumbers) {
    Console.WriteLine(number);
}

In this example, a List<int> is initialized with several integer values. The OrderBy method from LINQ is used to sort the list in ascending order, and then the resulting sorted list is printed to the console using a foreach loop.

Example 4: Sorting a Collection by DateTime in C#

class Person {
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
}

List<Person> people = new List<Person> {
    new Person { Name = "Alice", Birthday = new DateTime(2000, 1, 1) },
    new Person { Name = "Bob", Birthday = new DateTime(1990, 1, 1) },
    new Person { Name = "Charlie", Birthday = new DateTime(1980, 1, 1) },
    new Person { Name = "Dave", Birthday = new DateTime(1970, 1, 1) }
};

var sortedPeople = people.OrderBy(person => person.Birthday);

foreach (var person in sortedPeople) {
    Console.WriteLine("{0} ({1})", person.Name, person.Birthday);
}

In this example, a List<Person> is initialized with several Person objects, each with a Name and Birthday property. The OrderBy method from LINQ is used to sort the list of people by their Birthday property in ascending order, and then the resulting sorted list is printed to the console using a foreach loop.

Example 5: Sorting One List by Another List in C#

List<int> numbers = new List<int> { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
List<string> names = new List<string> { "Three", "One", "Four", "One", "Five", "Nine", "Two", "Six", "Five" };

var sortedNumbers = numbers.OrderBy(x => names[numbers.IndexOf(x)]);

foreach (var number in sortedNumbers) {
    Console.WriteLine(number);
}

In this example, a List<int> is initialized with several integer values, and a List<string> is initialized with corresponding string values. The OrderBy method from LINQ is used to sort the list of numbers based on the corresponding string values in the names list, using a lambda expression that looks up the index of each number in the numbers list and uses that index to look up the corresponding string value in the names list. The resulting sorted list of numbers is then printed to the console using a foreach loop.

  1. C# sort list example:

    List<int> numbers = new List<int> { 5, 3, 7, 1, 9 };
    numbers.Sort();
    
  2. Sort List of integers in C#:

    List<int> numbers = new List<int> { 5, 3, 7, 1, 9 };
    numbers.Sort();
    
  3. C# list sort ascending:

    List<int> numbers = new List<int> { 5, 3, 7, 1, 9 };
    numbers.Sort(); // Ascending order
    
  4. Sort List of strings alphabetically in C#:

    List<string> names = new List<string> { "John", "Jane", "Alice", "Bob" };
    names.Sort();
    
  5. C# list sort descending:

    List<int> numbers = new List<int> { 5, 3, 7, 1, 9 };
    numbers.Sort((a, b) => b.CompareTo(a)); // Descending order
    
  6. Sorting List of custom objects in C#:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    persons.Sort();
    
  7. C# list sort by property:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    persons.Sort((p1, p2) => p1.Name.CompareTo(p2.Name));
    
  8. Sort List of DateTime in C#:

    List<DateTime> dates = new List<DateTime> { /* List of DateTime objects */ };
    dates.Sort();
    
  9. C# list sort with comparer:

    List<string> names = new List<string> { "John", "Jane", "Alice", "Bob" };
    names.Sort(new MyStringComparer());
    
    // Custom comparer class
    public class MyStringComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            return x.CompareTo(y);
        }
    }
    
  10. Sort List of objects by multiple properties in C#:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    persons.Sort((p1, p2) => p1.Name.CompareTo(p2.Name) != 0 ? p1.Name.CompareTo(p2.Name) : p1.Age.CompareTo(p2.Age));
    
  11. C# list sort with lambda expression:

    List<int> numbers = new List<int> { 5, 3, 7, 1, 9 };
    numbers.Sort((a, b) => a.CompareTo(b));
    
  12. Sort List with LINQ OrderBy in C#:

    List<int> numbers = new List<int> { 5, 3, 7, 1, 9 };
    numbers = numbers.OrderBy(n => n).ToList();
    
  13. C# list sort by specific criteria:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    persons.Sort((p1, p2) => p1.Age.CompareTo(p2.Age));
    
  14. Sort List of floats in C#:

    List<float> floatNumbers = new List<float> { /* List of floats */ };
    floatNumbers.Sort();
    
  15. C# list sort using IComparer:

    List<float> floatNumbers = new List<float> { /* List of floats */ };
    floatNumbers.Sort(new MyFloatComparer());
    
    // Custom comparer class
    public class MyFloatComparer : IComparer<float>
    {
        public int Compare(float x, float y)
        {
            return x.CompareTo(y);
        }
    }
    
  16. Sort List of doubles in C#:

    List<double> doubleNumbers = new List<double> { /* List of doubles */ };
    doubleNumbers.Sort();
    
  17. C# list sort by key:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    persons.Sort((p1, p2) => p1.Age.CompareTo(p2.Age));
    
  18. Sort List of strings by length in C#:

    List<string> words = new List<string> { "apple", "banana", "grape", "kiwi" };
    words.Sort((a, b) => a.Length.CompareTo(b.Length));
    
  19. C# list sort with custom sorting logic:

    List<string> words = new List<string> { "apple", "banana", "grape", "kiwi" };
    words.Sort((a, b) => CustomCompare(a, b));
    
    // Custom comparison method
    private int CustomCompare(string a, string b)
    {
        // Implement custom sorting logic
        // ...
        return result;
    }