ASP.NET - How to Sort images name in listbox control

Asked By Reshma Gurav on 09-Jan-12 04:28 AM
I store multiple images path in listbox control.
protected void Button2_Click(object sender, EventArgs e)
    {
        string[] files = Directory.GetFiles("C:\\temp\\images", "*.jpg");
        foreach (string cfile in files)
        {
            ListBox3.Items.Add(cfile);
 
        }
    }
images names are 1.jpg,  2.jpg .... 10.jpg, 11.jpg

but when i run above statement the listbox control will display
1.jpg
10.jpg
11.jpg
...
2.jpg
20jpg
so how to sort the listbox control so that list will display like
1.jpg
2.jpg
....
10.jpg
11.jpg
smr replied to Reshma Gurav on 09-Jan-12 04:32 AM
hi

try in this way

Partial Class _Default
  Inherits System.Web.UI.Page

  Protected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles Button1.Click
    sortListBoxItems(lstGames)
  End Sub

  Private Sub sortListBoxItems(ByVal list As Object)

    ' Create Variables
    Dim li As ListItem
    Dim sl As SortedList = New SortedList

    ' Loop through each Item in the List and move them over to the SortedList
      For Each li In list.Items
      sl.Add(li.Text, li.Value)
    Next

    ' Move sorted items back to List again
    list.DataSource = sl
    list.DataValueField = "Value"
    list.DataTextField = "Key"
    list.DataBind()

  End Sub

End Class
follow
http://v4.vbasic.net/how-to-sort-items-in-a-listbox-in-visual-basic-net/
kalpana aparnathi replied to Reshma Gurav on 09-Jan-12 04:37 AM
hi,
private void SortListBox()
  {
    List<ListItem> t = new List<ListItem>();
    Comparison compare = new Comparison(CompareListItems);
    foreach (ListItem lbItem in lb1.Items)
      t.Add(lbItem);
 
    t.Sort(compare);
    lb1.Items.Clear();
    lb1.Items.AddRange(t.ToArray());
 
  }
 
  int CompareListItems(ListItem li1, ListItem li2)
  {
    return String.Compare(li1.Value, li2.Value);
  }
Suchit shah replied to Reshma Gurav on 09-Jan-12 04:39 AM
Here's an example that might help you out:
 
ASPX

<asp:listbox id="ListBox1" runat="server" rows="7">
    <asp:listitem>G</asp:listitem>
    <asp:listitem>B</asp:listitem>
    <asp:listitem>E</asp:listitem>
    <asp:listitem>D</asp:listitem>
    <asp:listitem>A</asp:listitem>
    <asp:listitem>F</asp:listitem>
    <asp:listitem>C</asp:listitem>
</asp:listbox>
<asp:button id="btnSort" runat="server" onclick="btnSort_Click" text="Sort" />

CODE-BEHIND
private int CompareListItemsByValue(ListItem x, ListItem y)
{
    return x.Value.CompareTo(y.Value);
}
  
protected void btnSort_Click(object sender, EventArgs e)
{
    List items = new List();
  
    foreach (ListItem li in ListBox1.Items)
    {
        items.Add(li);
    }
  
    items.Sort(CompareListItemsByValue);
  
    ListBox1.Items.Clear();
  
    foreach (ListItem li in items)
    {
        ListBox1.Items.Add(li);
    }
}
D Company replied to kalpana aparnathi on 09-Jan-12 04:41 AM
select numeric value from image name and than apply sorting on numeric value. will help you to get desired result
dipa ahuja replied to Reshma Gurav on 09-Jan-12 06:30 AM
void sort()
{
  //for sorting purpose
  List<int> s = new List<int>();
 
  //for adding items to it
  List<string> fileNames = new List<string>();
  string[] files = new string[4];
  files[0] = "1.jpg";
  files[1] = "10.jpg";
  files[2] = "12.jpg";
  files[3] = "2.jpg";
  foreach (string f in files)
  {
    //adding only filenames
    s.Add(int.Parse(f.Split('.')[0]));
  }
  //sorting
  s.Sort();
 
  foreach (int items in s)
  {
    //adding filenames with extension
    fileNames.Add(items + ".jpg"); 
  }
   
}