I've been playing with a sorter for layout tab names in a ListView. The problem I wanted to solve was I needed the Model tab to always be the first item in the list. To that end, I've built this IComparer to do the job. It only works correctly if you are sorting on a single column. Sorting on multiple columns produce unreliable results, but since I'm only using a single column, its of no consequence for me.
public class ListViewItemComparer : System.Collections.IComparer
{
private int col;
private SortOrder sort;
public ListViewItemComparer()
{
col = 0;
sort = SortOrder.Ascending;
}
public ListViewItemComparer(int column, SortOrder order)
{
col = column;
sort = order;
}
public int Compare(object x, object y)
{
string left = ((ListViewItem)x)[col].Text;
string right = ((ListViewItem)y)[col].Text;
if (lvx.SubItems[col].Text == "Model")
{
return -1;
}
else if (lvy.SubItems[col].Text == "Model")
{
return 1;
}
else
{
return (sort == SortOrder.Descending) ? (right.CompareTo(left)) : (left.CompareTo(right));
}
}
}