How to sort ListBox items by value in asp.net

asp.net listbox sort by value
The following asp.net c# example code demonstrate us the process to sort ListBox items programmatically based on items value.

ListBox control has no built in method or property to directly sort its items by its value. So to apply sorting on ListBox items, we need to take few techniques. First we Initializes a Generic List with data type ListItem and populate the Generic List with ListBox ListItems collection. Next we sort the Generic List items by OrderBy() method. We sort the Generic List items based on ListItem object's 'Value' property value. We sort the Generic List items on ascending order.

Next, we remove all items from ListBox server control and repopulate the ListBox with Generic List items. We can populate ListBox with Generic List items because the Generic List data type is ListItem.

ListBox items collection Addrange() method allow us to add multiple items to ListBox at a time. This method require to pass a ListItem object array as parameter. So we convert the Generic List to an Array object and pass it to AddRange() method. Finally the ListBox render its items as sorted based on ListItem value.
listbox-sort-by-value.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>    
      
<!DOCTYPE html>
        
<script runat="server">  
    protected void Page_Load(object sender, EventArgs e)  
    {   
        if(!Page.IsPostBack)
        {
            List<ListItem> list = new List<ListItem>(ListBox1.Items.Cast<ListItem>());
            
            //sort list item asxcending by value
            list = list.OrderBy(x=> x.Value).ToList<ListItem>();

            ListBox1.Items.Clear();
            ListBox1.Items.AddRange(list.ToArray<ListItem>());

            Label1.Text = "";
            foreach(ListItem li in ListBox1.Items)
            {
                Label1.Text += li.Text +" = "+ li.Value+ "<br />";
            }
        }
    }
</script>        
        
<html xmlns="http://www.w3.org/1999/xhtml">        
<head id="Head1" runat="server">        
    <title>asp.net listbox sort by value</title>  
</head>        
<body>        
    <form id="form1" runat="server">        
    <div>        
        <h2 style="color:MidnightBlue; font-style:italic;">        
            asp.net example - listbox sort by value
        </h2>        
        <hr width="550" align="left" color="Gainsboro" />        
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Text="select an item from ListBox."
            Font-Size="X-Large"
            Width="350"
            >      
        </asp:Label>      
        <br /><br />  
        <asp:ListBox  
            ID="ListBox1"  
            runat="server"  
            AutoPostBack="true"  
            Width="350"  
            Font-Size="X-Large"
            SelectionMode="Single"
            Height="200"  
            >  
            <asp:ListItem Text="Common Tody-flycatcher" Value="5"></asp:ListItem>
            <asp:ListItem Text="White-throated Spadebill" Value="3"></asp:ListItem>
            <asp:ListItem Text="Eastern Phoebe" Value="2"></asp:ListItem>
            <asp:ListItem Text="Keel-billed Toucan" Value="1"></asp:ListItem>
            <asp:ListItem Text="Greater Honeyguide" Value="4"></asp:ListItem>
            <asp:ListItem Text="Ochre-collared Piculet" Value="4"></asp:ListItem>
        </asp:ListBox>  
    </div>        
    </form>        
</body>        
</html>
More asp.net examples