you need to give it an actual instance of your Picture class, not the type. Also, your ListViewItemSorter will actually call the Comparer by passing it the ListViewItem not the Picture class, you can add an instance of the Picture class to the Tag property of your ListViewItem.
Something like this, this is a very rough implementation, just to give you an idea. You can implement your own error handling.
Picture test1 = new Picture() { Name = "Picture #1", Size = 54 };
Picture test2 = new Picture() { Name = "Picture #2", Size = 10 };
this.listView1.ListViewItemSorter = test1;
this.listView1.Items.Add(new ListViewItem(test1.Name) { Tag = test1 });
this.listView1.Items.Add(new ListViewItem(test2.Name) { Tag = test2 });
public class Picture : IComparer
{
public string Name { get; set; }
public int Size { get; set; }
#region IComparer Members
public int Compare(object x, object y)
{
Picture itemA = ((ListViewItem)x).Tag as Picture;
Picture itemB = ((ListViewItem)y).Tag as Picture;
if (itemA.Size < itemB.Size)
return -1;
if (itemA.Size > itemB.Size)
return 1;
if (itemA.Size == itemB.Size)
return 0;
return 0;
}
The output is:
Here is an MSDN Link