LINQ Ordering Operators


In LINQ, you can order results using the OrderBy, OrderByDescending, ThenBy, and ThenByDescending m methods. These methods allow you to sort collections based on one or more criteria. Here's an overview of each operator:

  1. OrderBy
  2. OrderByDescending
  3. ThenBy
  4. ThenByDescending
  5. Reverse

Here are examples to demonstrate their usage.

1. OrderBy Operator

In LINQ, OrderBy Operator is used to sorts the elements of a sequence in ascending order according to a key.

Example

using System;

namespace FirstProgram
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> people = new List<Person>
            {
               new Person { FirstName = "Rohatash", LastName = "Kumar" },
               new Person { FirstName = "Mohit", LastName = "Singh" },
               new Person { FirstName = "Saurav", LastName = "Kumar" }
            };

            // Order by LastName in ascending order
            var orderedByLastNameAsc = people.OrderBy(p => p.FirstName).ToList();
            foreach (var person in orderedByLastNameAsc)
            {
                 Console.WriteLine(person.FirstName);
            }
           Console.ReadKey();
        }
    }
}

Output

LINQ

2. OrderByDescending Operator

In LINQ, OrderByDescending Operator is used to sorts the elements of a sequence in descending order according to a key.

Example

using System;

namespace FirstProgram
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> people = new List<Person>
            {
               new Person { FirstName = "Rohatash", LastName = "Kumar" },
               new Person { FirstName = "Mohit", LastName = "Singh" },
               new Person { FirstName = "Saurav", LastName = "Kumar" }
            };

            // Order by LastName in descending order
             var orderedByLastNameDesc = people.OrderByDescending(p => p.FirstName).ToList();
            foreach (var person in orderedByLastNameDesc)
            {
                 Console.WriteLine(person.FirstName);
            }

           Console.ReadKey();
        }
    }
}

Output

LINQ

3. ThenBy Operator

In LINQ, ThenBy Operator is used to performs a subsequent ordering of the elements in a sequence in ascending order.

Example

using System;

namespace FirstProgram
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> people = new List<Person>
            {
               new Person { FirstName = "Rohatash", LastName = "Kumar" },
               new Person { FirstName = "Mohit", LastName = "Singh" },
               new Person { FirstName = "Saurav", LastName = "Kumar" }
            };

             // Order by LastName, then by FirstName in ascending order
                var orderedByLastThenFirstAsc = people
               .OrderBy(p => p.LastName)
                .ThenBy(p => p.FirstName)
               .ToList();

               foreach (var person in orderedByLastThenFirstAsc)
               {
                   Console.WriteLine(person.FirstName);
               }
               Console.ReadKey();
        }
    }
}

Output

LINQ

4. ThenByDescending Operator

In LINQ, ThenByDescending Operator is used to performs a subsequent ordering of the elements in a sequence in descending order.

Example

using System;

namespace FirstProgram
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> people = new List<Person>
            {
               new Person { FirstName = "Rohatash", LastName = "Kumar" },
               new Person { FirstName = "Mohit", LastName = "Singh" },
               new Person { FirstName = "Saurav", LastName = "Kumar" }
            };

            // Order by LastName, then by FirstName in desc order
            var ThenByDescending = people
            .OrderBy(p => p.LastName)
            .ThenByDescending(p => p.FirstName)
            .ToList();
           foreach (var person in ThenByDescending)
           {
              Console.WriteLine(person.FirstName);
           }
           Console.ReadKey();
        }
    }
}

Output

LINQ

5. Reverse Operator

In LINQ, if you want to reverse the order of the elements in a sequence, you can use the Reverse method. This method is used to reverse the order of the elements in a sequence, but it's typically used after an ordering operation if you want to reverse the order of an already sorted sequence.

Example

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

public class Program
{
    public static void Main()
    {
        var people = new[] { "Rohatash", "Rahul", "Mohit", "Saurav" };

        var resultstring = people.Reverse().ToList();
        foreach (var m in resultstring)
        {
            Console.WriteLine(m);
        }
    }
}

Output

LINQ
Prev Next