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.
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());
很难说出MyLIst是如何定义的,但是如果它是标准集合之一,则应该可以使用。
var result = MyList.OrderBy(x => x.expires).Select(x => x);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.