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:
Now, double click on Ascending Order and Descending Order buttons and write the following code:-
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:-
Now, we add the Default.aspx page and write the following code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
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:-