c# - How to sort elements of an ArrayList

ArrayList Sort() Method
.Net framework ArrayList.Sort() method allow us to sort the elements in the entire ArrayList. this arraylist Sort() method exists under System.Collections namespace. the Sort() method has no required or optional parameter.

this method throw NotSupportedException exception, if the arraylist is read-only. this method uses Array.Sort to sort the arraylist elements. this Array.Sort uses the QuickSort algorithm.

the following asp.net c# example code demonstrate us how can we sort the arraylist elements programmatically at run time in an asp.net application.
ArrayListSortMethod.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() { "LavenderBlush", "GreenYellow", "OliveDrab", "Coral", "Indigo","Cyan" };

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

        colors.Sort();

        Label1.Text += "<br /><br />After Call Sort() Method";
        Label1.Text += "<br />Now ArrayList Elements....";
        Label1.Text += "<font color=HotPink>";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }
        Label1.Text += "</font>";
    }
</script>

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