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()
            Asked
            
        
        
            Active
            
        
            Viewed 2,838 times
        
    2
            
            
        
        Soner Gönül
        
- 97,193
 - 102
 - 206
 - 364
 
        Quak
        
- 6,923
 - 4
 - 18
 - 22
 
- 
                    Did you tried anything so far? Please read [faq] and [ask] – Soner Gönül Jan 08 '13 at 18:35
 - 
                    1possible duplicate of [Sorting a list of items in a list box](http://stackoverflow.com/questions/3667088/sorting-a-list-of-items-in-a-list-box) – Soner Gönül Jan 08 '13 at 18:44
 - 
                    I also marked it as duplicate, but with closer look, it is slightly different as it involves `DateTime` – Tilak Jan 08 '13 at 19:07
 - 
                    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 Jan 08 '13 at 19:09
 
3 Answers
1
            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); 
}
        MethodMan
        
- 18,625
 - 6
 - 34
 - 52
 
- 
                    Do you think it is ethical to copies someone's answer? http://stackoverflow.com/a/3667107/447156 – Soner Gönül Jan 08 '13 at 18:58
 - 
                    @Soner I think that you are assuming this is my answer I am not sure why you would come out and accuse someone of using an answer.. – MethodMan Jan 08 '13 at 18:59
 - 
                    I could add an even more complex answer but I think for the OP that would be over Kill.. – MethodMan Jan 08 '13 at 19:03
 - 
                    4@DJKRAZE, I think you should read [is-it-okay-to-copy-paste-answers-from-other-questions](http://meta.stackexchange.com/questions/78658/is-it-okay-to-copy-paste-answers-from-other-questions). There is nothing wrong in copy as long as credit is going where it should go – Tilak Jan 08 '13 at 19:04
 
0
            
            
        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>());
        arunlalam
        
- 1,838
 - 2
 - 15
 - 23
 
- 
                    1Why convert the enumeration from `Cast` to a `List` when the only thing you're going to do with it is enumerate it with `OrderBy`? Why construct it with the constructor instead of `ToList` (which you do later)? Why do you convert the result of `OrderBy` into a `List` only to immediately convert it to an `Array`? Why are you supplying the generic parameters to `ToList` and `ToArray`? (They will be implied.) You can just use `var items = ListBox1.Items.Cast
().OrderByDecending(li => li.Value).ToArray()` then clear/addRange. – Servy Jan 08 '13 at 19:18 - 
                    @Servy Please would you either submit an answer or modify this one to correctly portray your thoughts? Thank you. – CaptainBli Aug 18 '14 at 21:24
 
-1
            
            
        try to sort a list of dates and after that put in the listbox.
dateList.Sort();
var items = new SelectList(dateList);