C# Program to order array elements

C# provides several methods to order array elements. The OrderBy() method sorts elements in ascending order, while ThenBy() is used for secondary sorting criteria when multiple elements have the same primary sort value.

Syntax

Following is the syntax for ordering array elements using LINQ methods −

// Primary ordering
IEnumerable<T> result = array.OrderBy(item => item.Property);

// Primary and secondary ordering  
IEnumerable<T> result = array.OrderBy(item => item.Property1)
                             .ThenBy(item => item.Property2);

// Descending order
IEnumerable<T> result = array.OrderByDescending(item => item.Property);

Using OrderBy() and ThenBy() for Multi-Level Sorting

The ThenBy() method provides a secondary sorting criterion. In this example, we first sort strings by length, then alphabetically for strings of the same length −

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        string[] str = { "Sandler", "Jack", "Tom", "Matt", "Henry", "Johnny" };
        
        IEnumerable<string> res = str.OrderBy(name => name.Length)
                                    .ThenBy(name => name);
        
        Console.WriteLine("Sorted by length, then alphabetically:");
        foreach (string name in res) {
            Console.WriteLine($"{name} ({name.Length} characters)");
        }
    }
}

The output of the above code is −

Sorted by length, then alphabetically:
Tom (3 characters)
Jack (4 characters)
Matt (4 characters)
Henry (5 characters)
Johnny (6 characters)
Sandler (7 characters)

Using Array.Sort() Method

The Array.Sort() method provides a simpler approach for basic sorting operations −

using System;

public class Demo {
    public static void Main() {
        int[] numbers = { 64, 34, 25, 12, 22, 11, 90 };
        string[] names = { "Charlie", "Alice", "Bob", "David" };
        
        Console.WriteLine("Original numbers: " + string.Join(", ", numbers));
        Array.Sort(numbers);
        Console.WriteLine("Sorted numbers: " + string.Join(", ", numbers));
        
        Console.WriteLine("\nOriginal names: " + string.Join(", ", names));
        Array.Sort(names);
        Console.WriteLine("Sorted names: " + string.Join(", ", names));
    }
}

The output of the above code is −

Original numbers: 64, 34, 25, 12, 22, 11, 90
Sorted numbers: 11, 12, 22, 25, 34, 64, 90

Original names: Charlie, Alice, Bob, David
Sorted names: Alice, Bob, Charlie, David

Using OrderByDescending() for Reverse Order

For descending order, use OrderByDescending() and ThenByDescending() methods −

using System;
using System.Linq;

public class Demo {
    public static void Main() {
        double[] scores = { 85.5, 92.3, 78.1, 92.3, 88.7, 95.2 };
        
        var sortedScores = scores.OrderByDescending(score => score);
        
        Console.WriteLine("Scores in descending order:");
        foreach (double score in sortedScores) {
            Console.WriteLine(score);
        }
    }
}

The output of the above code is −

Scores in descending order:
95.2
92.3
92.3
88.7
85.5
78.1

Comparison of Sorting Methods

Method Type Modifies Original Best For
Array.Sort() In-place sorting Yes Simple, fast sorting of primitive types
OrderBy() LINQ method No Complex sorting with custom criteria
ThenBy() LINQ method No Multi-level sorting with secondary criteria

Conclusion

C# offers multiple approaches to order array elements: Array.Sort() for simple in-place sorting, and LINQ methods like OrderBy() and ThenBy() for complex multi-level sorting. Choose the method based on whether you need to preserve the original array and the complexity of your sorting requirements.

Updated on: 2026-03-17T07:04:35+05:30

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements