LINQ (C#) – OrderBy & OrderByDescending Programming Example and Tutorial

In this tutorial, you will learn:
  1. What are OrderBy and OrderByDescending Operator in LINQ?
  2. How to use OrderBy operator to sort result?
  3. How to use OrderByDescending to sort result?
  4. Programming Example

OrderBy and OrderByDescending, both are a sorting operator that sorts the elements in a collection. OrderBy sorts elements in ascending order, whereas OrderByDescending Operator sorts element in descending order.

OrderBy Programming Example

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

namespace LinqTutorial
{
    class ProductStore
    {
        public string productName { get; set; }
        public int productPrice { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Creating List
            IList<ProductStore> productList = new List<ProductStore>();

            productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000 });
            productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500 });
            productList.Add(new ProductStore { productName = "RAM", productPrice = 2450 });
            productList.Add(new ProductStore { productName = "Processor", productPrice = 7680 });
            productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540 });
            productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130 });

            //LINQ Query Syntax
            var result = from p in productList
                         orderby p.productPrice
                         select p;

            //LINQ Method Syntax. Uncomment it to see the result.
            //var result = productList.OrderBy(p => p.productPrice);

            foreach (var list in result)
            {
                Console.WriteLine("Product Name: {0} | Product Price : {1}", list.productName, list.productPrice);
            }

            Console.ReadKey();
        }
    }
}

Output:


Product Name: Bluetooth | Product Price : 540
Product Name: Keyboard & Mouse | Product Price : 1130
Product Name: Hard Disk | Product Price : 1280
Product Name: RAM | Product Price : 2450
Product Name: Monitor | Product Price : 3000
Product Name: SSD Disk | Product Price : 3500
Product Name: Processor | Product Price : 7680

_

OrderByDescending Programming Example

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

namespace LinqTutorial
{
    class ProductStore
    {
        public string productName { get; set; }
        public int productPrice { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Creating List
            IList<ProductStore> productList = new List<ProductStore>();

            productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000 });
            productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500 });
            productList.Add(new ProductStore { productName = "RAM", productPrice = 2450 });
            productList.Add(new ProductStore { productName = "Processor", productPrice = 7680 });
            productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540 });
            productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130 });

            //LINQ Query Syntax
            var result = from p in productList
                         orderby p.productPrice descending
                         select p;

            //LINQ Method Syntax. Uncomment it to see the result.
            //var result = productList.OrderByDescending(p => p.productPrice);

            foreach (var list in result)
            {
                Console.WriteLine("Product Name: {0} | Product Price : {1}", list.productName, list.productPrice);
            }

            Console.ReadKey();
        }
    }
}


Output

Product Name: Processor | Product Price : 7680
Product Name: SSD Disk | Product Price : 3500
Product Name: Monitor | Product Price : 3000
Product Name: RAM | Product Price : 2450
Product Name: Hard Disk | Product Price : 1280
Product Name: Keyboard & Mouse | Product Price : 1130
Product Name: Bluetooth | Product Price : 540

_

Summary

This tutorial teaches you how to use OrderBy and OrderByDescending LINQ Operator to sort list in ascending order and descending order. In the next chapter, you will learn ThenBy operator in C#.

 

Share your thought