Generic List Sort() method in asp.net c#

Generic List Sort() Method
.Net framework generic list Sort() method sorts the elements in the entire List<T>. Sort() method use the default comparer. this method exists in under System.Collections.Generic namespace. the method has no parameter. Sort() method has only one exception named InvalidOperationException.

the Sort() method uses the Array.Sort method to sort the generic list elements. Sort() method uses the default comparer Comparer<T>.Default for type 'T' to determine the order of list elements.

the following asp.net c# example code demonstrate us how can we sort generic list elements in an asp.net application.
GenericListSortMethod.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        List<string> colors = new List<string>() {"CadetBlue","SeaGreen","Olive"};
        colors.Add("IndianRed");
        colors.Add("Blue");
        colors.Add("SeaGreen");

        Label1.Text = "List Elements....<br />";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }

        colors.Sort();
        Label1.Text += "<br /><br />After Call The Sort() Method";
        Label1.Text += "<br />Now List Elements....<br />";

        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Generic List Sort()- How to sort the elements in the entire List</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Purple; font-style:italic;">
            System.Collections.Generic.List Sort() Method
            <br /> How to sort the elements in the entire List
        </h2>
        <hr width="450" align="left" color="Orchid" />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="RoyalBlue"
             Font-Size="Large"
             Font-Names="Courier New"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Generic List Sort() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples