How can I insert an item into a list in alphabetical order?

I am creating a web page in which I show the list items that are retrieved from the database. Dynamically, I added some elements to it. It adds to the end of the list box, so I want to sort the list item after adding items. I tried Arraylist to sort, but it does not work.

+8
source share
8 answers

I was looking for a way to do this without comparison classes, ArrayLists, etc. So I decided to insert a method that I used for others:

Please note that my method uses LINQ to Objects, so it will only work in newer versions of the Framework.

 // get a LINQ-enabled list of the list items List<ListItem> list = new List<ListItem>(ListBoxToSort.Items.Cast<ListItem>()); // use LINQ to Objects to order the items as required list = list.OrderBy(li => li.Text).ToList<ListItem>(); // remove the unordered items from the listbox, so we don't get duplicates ListBoxToSort.Items.Clear(); // now add back our sorted items ListBoxToSort.Items.AddRange(list.ToArray<ListItem>()); 
+22
source share

Try this code in the PreRender event of your ListBox .

 System.Collections.SortedList sorted = new SortedList(); foreach (ListItem ll in ListBox1.Items) { sorted.Add(ll.Text, ll.Value); } ListBox1.Items.Clear(); foreach (String key in sorted.Keys) { ListBox1.Items.Add(new ListItem(key, sorted[key].ToString())); } 
+3
source share

Try using the following code:

  DataTable dt = new DataTable(); dt.Columns.Add("val", Type.GetType("System.Int32")); DataRow dr; for (int i = 1; i <= 31; i++) { dr = dt.NewRow(); dr[0] = i; dt.Rows.Add(dr); } dt.AcceptChanges(); DataView dv = dt.DefaultView; dv.Sort = "val desc"; ddlDay.DataTextField = "val"; ddlDay.DataValueField = "val"; ddlDay.DataSource = dv.ToTable(); ddlDay.DataBind(); 

If ur links the list, setting the datasource as a DataTable filled with db data, then when you want to add a new element instead of adding an element, add an entry to the datatable. Then create a dataview for that datatable, sort the data in a dataview as shown below:

 DataView dv = dt.DefaultView; dv.Sort = "val desc"; 

then set the list data source as dv.ToTable()

+1
source share

You must sort the data before assigning it to a data source in the list box.

 DataTable.DefaultView.sort = "[Column Name]". 

As you mentioned that you use the Add method to add ListItems to a List, the Add method always adds ListItems. You can add items at the desired index using the Insert method with the index. List.Insert (ListItem, index)

0
source share

I know this is very similar to The King's answer, but it was so for me.

 System.Collections.SortedList sorted = new System.Collections.SortedList(); foreach (ListItem ll in ListInQuestion.Items) { sorted.Add(ll.Value, ll.Text);//yes, first value, then text } ListInQuestion.Items.Clear(); foreach (String key in sorted.Keys) { ListInQuestion.Items.Add(new ListItem(sorted[key].ToString(), key));// <-- look here! } 

I hope this works for someone else.

0
source share

Are you dynamically saying that you add items to the list through Javascript or after you return somekind using ASP.NET controls?

If you are adding dynamically using Javascript, then your answer will be sorted using jQuery, and if after postback using ASP.NET you should use the List object located in System.Collections.Generic.

-one
source share

Just call ArrayList.Sort . Sample code in the link.

-one
source share

Here is the VB code

 Private Sub SortList(ByVal lb As DropDownList) ' get a LINQ-enabled list of the list items Dim list As New List(Of ListItem)(lb.Items.Cast(Of ListItem)()) ' use LINQ to Objects to order the items as required list = list.OrderBy(Function(li) li.Text).ToList() ' remove the unordered items from the listbox, so we don't get duplicates lb.Items.Clear() ' now add back our sorted items lb.Items.AddRange(list.ToArray()) End Sub 
-one
source share

All Articles