Reputation: 13
I would like to ask how to sort list by date time. I have class task
public class tasks
{
public string name { get; set; }
/// <summary>
/// Datum narození
/// </summary>
public DateTime expires { get; set; }
.
.
.
}
class data
public class data
{
public List<tasks> tsk = new List<tasks>();
public data()
{
}
public void AddTask(string name, DateTime expires)
{
tasks ts = new tasks(name, expires.Date);
tsk.Add(ts);
}
In Main program, I stores data in a List.
private data dt = new data();
dt.AddTask(textBox1.Text, dateTimePicker1.Value);
I would like to write data to the listbox sortAscending.
Upvotes: 0
Views: 107
Reputation: 70523
It is hard to tell how MyLIst is defined, but if it is one of the standard collections this should work.
var result = MyList.OrderBy(x => x.expires).Select(x => x);
Upvotes: 0
Reputation: 38077
You can sort using LINQ and then place it into the list box using AddRange:
MyList m = new MyList();
m.Add("n1", DateTime.Now);
m.Add("n2", DateTime.Now.AddDays(1));
m.Add("n3", DateTime.Now.AddDays(-1));
var sortedList = from i in m
orderby i.expires
select i;
listBox1.Items.AddRange(sortedList.ToArray());
Upvotes: 2