LINQ Sorting Operators

In Linq, sorting operators are used when we have to change the sequence or order of the data. i.e. Ascending sort or Descending sort. There are many sorting operators like OrderBy(), OrderByDescending(), ThenBy(), ThenByDescending() etc.Each has its unique use.

Linq OrderBy operator

It is used to order the data in ascending order and it is the default sort done by it.

Syntax:

var data = user.OrderBy(x => x.UserId);

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            List<User> user = new List<User>()
            {
                new User() { UserId = 1, Name = "Faisal", Age = 21 },
                new User() { UserId = 5, Name = "Irshad", Age = 18 },
                new User() { UserId = 4, Name = "Bhavdip", Age = 28 },
                new User() { UserId = 2, Name = "Kaushik", Age = 28 },
                new User() { UserId = 3, Name = "Kirti", Age = 25 }
            };
            var data = user.OrderBy(x => x.UserId);
            foreach (var item in data)
            {
                Console.WriteLine("The User name is {0} ID is {1} Age is {2}", item.Name, item.UserId, item.Age);
            }
            Console.ReadLine();
        }
        public class User
        {
            public int UserId { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}

As you can see all the data are in ascending order and sorted by UserId

Output:

The User name is Faisal ID is 1 Age is 21
The User name is Kaushik ID is 2 Age is 28
The User name is Kirti ID is 3 Age is 25
The User name is Bhavdip ID is 4 Age is 28
The User name is Irshad ID is 5 Age is 18

Linq OrderByDescending operator

This is used to order the data in descending order

Syntax:

var data = user.OrderByDescending(x => x.UserId);

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            List<User> user = new List<User>()
            {
                new User() { UserId = 1, Name = "Faisal", Age = 21 },
                new User() { UserId = 5, Name = "Irshad", Age = 18 },
                new User() { UserId = 4, Name = "Bhavdip", Age = 28 },
                new User() { UserId = 2, Name = "Kaushik", Age = 28 },
                new User() { UserId = 3, Name = "Kirti", Age = 25 }
            };
            var data = user.OrderByDescending(x => x.UserId);
            foreach (var item in data)
            {
                Console.WriteLine("The User name is {0} ID is {1} Age is {2}", item.Name, item.UserId, item.Age);
            }
            Console.ReadLine();
        }
        public class User
        {
            public int UserId { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}

Output:

The User name is Irshad ID is 5 Age is 18
The User name is Bhavdip ID is 4 Age is 28
The User name is Kirti ID is 3 Age is 25
The User name is Kaushik ID is 2 Age is 28
The User name is Faisal ID is 1 Age is 21

LINQ ThenBy Sorting Operator

In Linq, ThenBy operator is used to implementing sorting on multiple fields. It will sort the fields in ascending order by default. Generally, it is used with OrderBy operator to implement multiple fields sorting.

If we want to perform sorting on more than one field then we use the ThenBy operator.

Syntax:

var data = user.OrderBy(x => x.Age).ThenBy(x=>x.UserId);

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            List<User> user = new List<User>()
            {
                new User() { UserId = 1, Name = "Faisal", Age = 21 },
                new User() { UserId = 5, Name = "Irshad", Age = 18 },
                new User() { UserId = 4, Name = "Bhavdip", Age = 28 },
                new User() { UserId = 2, Name = "Kaushik", Age = 28 },
                new User() { UserId = 3, Name = "Kirti", Age = 25 }
            };
            var data = user.OrderBy(x => x.Age).ThenBy(x=>x.UserId);
            foreach (var item in data)
            {
                Console.WriteLine("The User name is {0} ID is {1} Age is {2}", item.Name, item.UserId, item.Age);
            }
            Console.ReadLine();
        }
        public class User
        {
            public int UserId { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}

Output:

The User name is Irshad ID is 5 Age is 18
The User name is Faisal ID is 1 Age is 21
The User name is Kirti ID is 3 Age is 25
The User name is Kaushik ID is 2 Age is 28
The User name is Bhavdip ID is 4 Age is 28

LINQ ThenByDescending Sorting Operator

In Linq, ThenByDescending operator is used to implementing sorting on multiple fields. It will sort the fields in Descending order by default. Generally, it is used with OrderBy operator to implement multiple fields sorting.

If we want to perform sorting on more than one field then we use the ThenBy operator.

Syntax:

var data = user.OrderByDescending(x => x.Age).ThenBy(x=>x.UserId);

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            List<User> user = new List<User>()
            {
                new User() { UserId = 1, Name = "Faisal", Age = 21 },
                new User() { UserId = 5, Name = "Irshad", Age = 18 },
                new User() { UserId = 4, Name = "Bhavdip", Age = 28 },
                new User() { UserId = 2, Name = "Kaushik", Age = 28 },
                new User() { UserId = 3, Name = "Kirti", Age = 25 }
            };
            var data = user.OrderByDescending(x => x.Age).ThenBy(x=>x.UserId);
            foreach (var item in data)
            {
                Console.WriteLine("The User name is {0} ID is {1} Age is {2}", item.Name, item.UserId, item.Age);
            }
            Console.ReadLine();
        }
        public class User
        {
            public int UserId { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}

Output:

The User name is Irshad ID is 5 Age is 18
The User name is Faisal ID is 1 Age is 21
The User name is Kirti ID is 3 Age is 25
The User name is Bhavdip ID is 4 Age is 28
The User name is Kaushik ID is 2 Age is 28

2 Comments

  1. baccarat

    Love watch Ships !

    0
    0
    Reply
  2. baccarat

    Love sand and sand clock !

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories