Sorting A ListView By Column
If you are starting out with a ListView, do yourself a huge favour and use an ObjectListView instead. ObjectListView is an open source wrapper around .NET WinForms ListView, which makes the ListView much easier to use and solves lots of common problems for you. Sorting by column click is one of the many things it handles for you automatically.
Seriously, you will never regret using an ObjectListView instead of a normal ListView.
- ObjectListView Home Page
- ObjectListView Nuget Package
I sort using column name to set any sorting specifics that may need to be handled based on data type stored in the column and or if the column has already been sorted on(asc/desc). Here's a snippet from my ColumnClick event handler.
private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
{
ListViewItemComparer sorter = GetListViewSorter(e.Column);
listView.ListViewItemSorter = sorter;
listView.Sort();
}
private ListViewItemComparer GetListViewSorter(int columnIndex)
{
ListViewItemComparer sorter = (ListViewItemComparer)listView.ListViewItemSorter;
if (sorter == null)
{
sorter = new ListViewItemComparer();
}
sorter.ColumnIndex = columnIndex;
string columnName = packagedEstimateListView.Columns[columnIndex].Name;
switch (columnName)
{
case ApplicationModel.DisplayColumns.DateCreated:
case ApplicationModel.DisplayColumns.DateUpdated:
sorter.ColumnType = ColumnDataType.DateTime;
break;
case ApplicationModel.DisplayColumns.NetTotal:
case ApplicationModel.DisplayColumns.GrossTotal:
sorter.ColumnType = ColumnDataType.Decimal;
break;
default:
sorter.ColumnType = ColumnDataType.String;
break;
}
if (sorter.SortDirection == SortOrder.Ascending)
{
sorter.SortDirection = SortOrder.Descending;
}
else
{
sorter.SortDirection = SortOrder.Ascending;
}
return sorter;
}
Below is my ListViewItemComparer
public class ListViewItemComparer : IComparer
{
private int _columnIndex;
public int ColumnIndex
{
get
{
return _columnIndex;
}
set
{
_columnIndex = value;
}
}
private SortOrder _sortDirection;
public SortOrder SortDirection
{
get
{
return _sortDirection;
}
set
{
_sortDirection = value;
}
}
private ColumnDataType _columnType;
public ColumnDataType ColumnType
{
get
{
return _columnType;
}
set
{
_columnType = value;
}
}
public ListViewItemComparer()
{
_sortDirection = SortOrder.None;
}
public int Compare(object x, object y)
{
ListViewItem lviX = x as ListViewItem;
ListViewItem lviY = y as ListViewItem;
int result;
if (lviX == null && lviY == null)
{
result = 0;
}
else if (lviX == null)
{
result = -1;
}
else if (lviY == null)
{
result = 1;
}
switch (ColumnType)
{
case ColumnDataType.DateTime:
DateTime xDt = DataParseUtility.ParseDate(lviX.SubItems[ColumnIndex].Text);
DateTime yDt = DataParseUtility.ParseDate(lviY.SubItems[ColumnIndex].Text);
result = DateTime.Compare(xDt, yDt);
break;
case ColumnDataType.Decimal:
Decimal xD = DataParseUtility.ParseDecimal(lviX.SubItems[ColumnIndex].Text.Replace("$", string.Empty).Replace(",", string.Empty));
Decimal yD = DataParseUtility.ParseDecimal(lviY.SubItems[ColumnIndex].Text.Replace("$", string.Empty).Replace(",", string.Empty));
result = Decimal.Compare(xD, yD);
break;
case ColumnDataType.Short:
short xShort = DataParseUtility.ParseShort(lviX.SubItems[ColumnIndex].Text);
short yShort = DataParseUtility.ParseShort(lviY.SubItems[ColumnIndex].Text);
result = xShort.CompareTo(yShort);
break;
case ColumnDataType.Int:
int xInt = DataParseUtility.ParseInt(lviX.SubItems[ColumnIndex].Text);
int yInt = DataParseUtility.ParseInt(lviY.SubItems[ColumnIndex].Text);
return xInt.CompareTo(yInt);
break;
case ColumnDataType.Long:
long xLong = DataParseUtility.ParseLong(lviX.SubItems[ColumnIndex].Text);
long yLong = DataParseUtility.ParseLong(lviY.SubItems[ColumnIndex].Text);
return xLong.CompareTo(yLong);
break;
default:
result = string.Compare(
lviX.SubItems[ColumnIndex].Text,
lviY.SubItems[ColumnIndex].Text,
false);
break;
}
if (SortDirection == SortOrder.Descending)
{
return -result;
}
else
{
return result;
}
}
}
Forget about your custom sorter. Start over using the code at the following page. It will show you how to define a class that inherits from the IComparer interface. Each line is commented out, so you can actually see what is happening. The only potential complication is how you are retrieving your Listview Items from your Listview control. Get those squared away and all you need to do is copy and paste the IComparer interface class and the columnClick method.
http://support.microsoft.com/kb/319401