// https://youtu.be/Wyfy9bfxRpk
// ListComparer.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ListViewSample
{
class ListComparer : IComparer
{
private int Col ;
private bool Inc = true ;
static int LastC = -1;
static bool LastInc = true ;
public ListComparer(int column)
{
Col = column ;
Inc = (LastC == Col) ? !LastInc : true ; // same column click for toggle each time
LastInc = Inc ;
LastC = Col ;
}
public int Compare(object x, object y)
{
string xStr = ((ListViewItem)x).SubItems[Col].Text ;
string yStr = ((ListViewItem)y).SubItems[Col].Text ;
int xV=0, yV=0 ;
bool num1 = int.TryParse(xStr, out xV) ;
bool num2 = int.TryParse(yStr, out yV) ;
if(num1 && num2) // If both are integers
{
if(Inc) return (xV>yV) ? 1 : -1;
else return (xV>yV) ? -1 : 1 ;
}
else // string compare
{
if(Inc)
return string.Compare(((ListViewItem)x).SubItems[Col].Text,
((ListViewItem)y).SubItems[Col].Text) ;
else
return string.Compare(((ListViewItem)y).SubItems[Col].Text,
((ListViewItem)x).SubItems[Col].Text) ;
}
}
}
}
// Form1.cs
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
listView1.ListViewItemSorter = new ListComparer(e.Column) ;
}