2
votes

Is there any easy way to sort listbox items by DateTime values? In my listbox1 I have items formatted like this: "2013.01.08 19:29:52" so it's just someDateTimeValue.ToString()

3
Did you tried anything so far? Please read faq and How to AskSoner Gönül
I also marked it as duplicate, but with closer look, it is slightly different as it involves DateTimeTilak
If your DateTime format is yyyy.MM.dd hh:mm:tt then default sorting(Array.Sort) will work. Otherwise you need to first parse it as DateTime, then do the sorting.Tilak

3 Answers

1
votes

if you have 2 ListBoxes you could do something like the following

ArrayList arList = new ArrayList(); 
foreach (object obj in listBox1.Items)
{
    arList.Add(obj);
} 
arList.Sort(); 
listBox2.Items.Clear();
foreach(object obj in arList)
{
    listBox2.Items.Add(obj); 
}
0
votes

Try this

List<ListItem> myList = new List<ListItem>(ListBox1.Items.Cast<ListItem>());        
myList = myList.OrderByDescending(li => li.Value).ToList<ListItem>();
ListBox1.Items.Clear();
ListBox1.Items.AddRange(myList.ToArray<ListItem>());
-1
votes

try to sort a list of dates and after that put in the listbox.

dateList.Sort();
var items = new SelectList(dateList);