I have a ListView to show some images (using ImageList). Everything works fine so far. I can sort items by theire Text property easily. But I want to be able to sort them by theire creation date as well (ASC or DESC).
Items are created like this:
Item.Text = file name
item.Tag = file path
I am trying to sort the date like this but no success in sorting the dictionary, and also I have no idea how to cast that dictionary items back into listview:
private void menuViewSortDate_Click(object sender, EventArgs e)
{
var files = new Dictionary<ListViewItem, DateTime>();
foreach(ListViewItem item in listViewLoadedImages.Items)
{
files.Add(item, File.GetCreationTime(item.Tag.ToString()));
}
listViewLoadedImages.BeginUpdate();
listViewLoadedImages.Items.Clear();
// how to sort dictionary by DateTime ??
// like files.Sort(bydate);
//How can refer to key values (listviewitems) in dictionary
listViewLoadedImages.Items.AddRange((ListViewItem)files.Keys);
listViewLoadedImages.EndUpdate();
}
As Avner says a dictionary is not ordered by default however you could use some LINQ to return a IEnumerable by doing:
I’m also not sure how you can make a ListViewItem unique as your key however you could try and cast your dictionary keys