How to sort a ListView control by a column in Visual C#
Posted
by bconlon
on Geeks with Blogs
See other posts from Geeks with Blogs
or by bconlon
Published on Mon, 28 Feb 2011 17:55:45 GMT
Indexed on
2011/02/28
23:26 UTC
Read the original article
Hit count: 398
Microsoft provide an article of the same name (previously published as Q319401) and it shows a nice class 'ListViewColumnSorter ' for sorting a standard ListView when the user clicks the column header.
This is very useful for String values, however for Numeric or DateTime data it gives odd results. E.g. 100 would come before 99 in an ascending sort as the string compare sees 1 < 9. So my challenge was to allow other types to be sorted. This turned out to be fairly simple as I just needed to create an inner class in ListViewColumnSorter which extends the .Net CaseInsensitiveComparer class, and then use this as the ObjectCompare member's type.
Note: Ideally we would be able to use IComparer as the member's type, but the Compare method is not virtual in CaseInsensitiveComparer , so we have to create an exact type:
public class ListViewColumnSorter : IComparer
{
private CaseInsensitiveComparer ObjectCompare;
private MyComparer ObjectCompare;
... rest of Microsofts class implementation...
}
Here is my private inner comparer class, note the 'new int Compare' as Compare is not virtual, and also note we pass the values to the base compare as the correct type (e.g. Decimal, DateTime) so they compare correctly:
You could extend this for other types, even custom classes as long as they support ICompare.
Microsoft also have another article How to: Sort a GridView Column When a Header Is Clicked that shows this for WPF, which looks conceptually very similar. I need to test it out to see if it handles non-string types.
#
© Geeks with Blogs or respective owner