top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

asp.net : How to sort items alphabetically in a combobox or dropdownlist ?

+1 vote
533 views
asp.net : How to sort items alphabetically in a combobox or dropdownlist ?
posted Jun 13, 2014 by Saif Khanam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

you can Sort in the query level itself by order by clause.its good and best practice also, But if you want to sort in the client side here is the function

private void SortDropDown(DropDownList dd) 
{ 
    ListItem[] ar = null; 
    long i = 0; 
    foreach (ListItem li in dd.Items) { 
        Array.Resize(ref ar, i + 1); 
        ar(i) = li; 
        i += 1; 
    } 
    Array ar1 = ar; 

    ar1.Sort(ar1, new ListItemComparer()); 
    dd.Items.Clear(); 
    dd.Items.AddRange(ar1); 
}

Compare class

private class ListItemComparer : IComparer 
{ 

    public int Compare(object x, object y) 
    { 
        ListItem a = x; 
        ListItem b = y; 
        CaseInsensitiveComparer c = new CaseInsensitiveComparer(); 
        return c.Compare(a.Text, b.Text); 
    } 
} 
answer Jun 16, 2014 by Mishthy Mukherjee
...