You can use a custom ListViewItemSorter to sort the columns of a ListView control.
This is an custom overloaded IComparer that lets you specify 1 or 2 columns for sorting, with the option to pass no parameters for a default Column[0]
order.
Specify a sort order:
listView1.Sorting = SortOrder.Ascending;
...and the Indexes of the Columns you want to compare. The order in which you enter them is of course important. In you example you should enter:
listView1.ListViewItemSorter = new ListViewItemComparer(1, 2);
The Default sorting Column[0] can be set like this:
listView1.ListViewItemSorter = new ListViewItemComparer();
The ListViewItemComparer
Class:
class ListViewItemComparer : IComparer
{
private int col1 = -1;
private int col2 = -1;
public ListViewItemComparer() => col1 = 0;
public ListViewItemComparer(int Column) => col1 = Column;
public ListViewItemComparer(int Column1, int Column2)
{
col1 = Column1;
col2 = Column2;
}
public int Compare(object x, object y)
{
int result = string.Compare(((ListViewItem)x).SubItems[col1].Text,
((ListViewItem)y).SubItems[col1].Text);
if (!(col2 < 0))
result |= string.Compare(((ListViewItem)x).SubItems[col2].Text,
((ListViewItem)y).SubItems[col2].Text);
return result;
}
}