Using sorting operators
The OrderBy and OrderByDescending operators are used to sort the data.

Now, we add the Default.aspx page and write the following code:

  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <center> 
    <div>
        <h1>Using sorting operators in LINQ:- </h1>
    <table>
        <tr> <td colspan="2"> <asp:ListBox ID="lbDetail" runat="server" Height="300px" Width="400px"></asp:ListBox> </td></tr>
        <tr> <td>
            <asp:Button ID="btnAscendingOrder" runat="server" Text="Ascending Order" Font-Bold="true" OnClick="btnAscendingOrder_Click"/> </td> <td><asp:Button ID="btnDescendingOrder" runat="server" Text="Descending Order" Font-Bold="true" OnClick="btnDescendingOrder_Click" /></td></tr>
    </table>
    </div>
            </center>
    </form>
</body>
</html>




Now, double click on Ascending Order and Descending Order buttons and write the following code:-

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default : System.Web.UI.Page
{
    int[] number = { 200, 100, 500, 300, 800, 900, 400, 600, 1000, 7000, 10000, 6000 };
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAscendingOrder_Click(object sender, EventArgs e)
    {
        lbDetail.Items.Clear();

        var orderBy = from d in number orderby d select d;
        foreach (var data in orderBy)
        {
            lbDetail.Items.Add(data.ToString());
        }
    }
    protected void btnDescendingOrder_Click(object sender, EventArgs e)
    {
        lbDetail.Items.Clear();

        var orderBy = from d in number orderby d descending select d;
        foreach (var data in orderBy)
        {
            lbDetail.Items.Add(data.ToString());
        }
    }
}



Now, run the application and click on Ascending Order and Descending Order buttons respectively and get output as following:-

Ascending Order Output:-


Descending Order Output:-