C# Source Code: Sorting Listview Columns By data type using the ListViewItemSorter property
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Sorting Listview Columns By data type using the ListViewItemSorter property
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, July 19, 2005
Hits:
2339
Category:
Windows Forms/GUI/Controls
Article:
Below is a class which is useful for performing custom sorting on a Listview control. The class handles three levels of sorting and different data types. using System; using System.Windows.Forms; using System.Collections; namespace VBusers.Windows.Forms.Controls { ///
/// Class to handle multiple column (max three columns) and data type list view sorting. /// To use, set the "ListViewItemSorter" property of a ListView control to an /// instance of the this class. ///
///
This example assumes a listview exists called lvDemo that contains dates in column 1: /// // Create listview custom sorting class /// ListViewColumnSorter listViewSorter = new ListViewColumnSorter(); /// // Set the primary sort to column 1 /// listViewSorter.PrimaryIndex = 1; /// // Set the primary sort ascending /// listViewSorter.PrimarySortOrder = SortOrder.Ascending; /// // Set the primary sort type to sort dates /// listViewSorter.PrimarySortType = ListViewSortType.SortDate; /// // Set the listview ListViewItemSorter property to the custom sort class /// lvDemo.ListViewItemSorter = listViewSorter; /// // Sort the items in the listview /// lvDemo.Sort(); ///
[Serializable] public class ListViewColumnSorter : IComparer { #region ListViewSortType Enum (ListViewColumnSorter) ///
/// Enumeration class used by ListViewColumnSorter. ///
public enum ListViewSortType: int { ///
Sort column containing string values.
SortString, ///
Sort column containing integer values.
SortInt32, ///
Sort column containing floating point values.
SortFloat, ///
Sort column containing date values.
SortDate } #endregion #region Private Variables private int _primaryIndex = 0; private SortOrder _primarySortOrder = SortOrder.Ascending; private int _secondaryIndex = 0; private SortOrder _secondarySortOrder = SortOrder.None; private int _tertiaryIndex = 0; private SortOrder _tertiarySortOrder = SortOrder.None; private bool _enabled = true; private ListViewSortType _primarySortType = ListViewSortType.SortString; private ListViewSortType _secondarySortType = ListViewSortType.SortString; private ListViewSortType _tertiarySortType = ListViewSortType.SortString; #endregion #region Public Properties ///
/// Gets or sets whether sorting is enabled. ///
public bool Enabled { get{ return _enabled; } set{ _enabled = value; } } ///
/// Returns a string representation of the object. ///
///
Returns a string representation of the object.
public override string ToString() { string sort = string.Empty; if ( _primaryIndex > -2 ) { sort += "Primary Column " + _primaryIndex.ToString() + " " + _primarySortType.ToString() + ". "; } if ( _secondaryIndex > -2 ) { sort += "Secondary Column " + _secondaryIndex.ToString() + " " + _secondarySortType.ToString() + ". "; } if ( _tertiaryIndex > -2 ) { sort += "Tertiary Column " + _tertiaryIndex.ToString() + " " + _tertiarySortType.ToString() + ". "; } return sort; } ///
/// Gets or sets the primary index of the column to sort (-1 indicates sort by Tag, -2 indicates no sort). ///
public int PrimaryIndex { get{ return _primaryIndex; } set{ _primaryIndex = value; } } ///
/// Gets or sets the primary sort order. ///
public SortOrder PrimarySortOrder { get{ return _primarySortOrder; } set{ _primarySortOrder = value; } } ///
/// Gets or sets the type of sorting to perform on the primary sort column. ///
public ListViewSortType PrimarySortType { get{ return _primarySortType; } set{ _primarySortType = value; } } ///
/// Gets or sets the secondary index of the column to sort (-1 indicates sort by Tag, -2 indicates no sort). ///
public int SecondaryIndex { get{ return _secondaryIndex; } set{ _secondaryIndex = value; } } ///
/// Gets or sets the secondary sort order. ///
public SortOrder SecondarySortOrder { get{ return _secondarySortOrder; } set{ _secondarySortOrder = value; } } ///
/// Gets or sets the type of sorting to perform on the secondary sort column. ///
public ListViewSortType SecondarySortType { get{ return _secondarySortType; } set{ _secondarySortType = value; } } ///
/// Gets or sets the tertiary index of the column to sort (-1 indicates sort by Tag, -2 indicates no sort). ///
public int TertiaryIndex { get{ return _tertiaryIndex; } set{ _tertiaryIndex = value; } } ///
/// Gets or sets the tertiary sort order. ///
public SortOrder TertiarySortOrder { get{ return _tertiarySortOrder; } set{ _tertiarySortOrder = value; } } ///
/// Gets or sets the type of sorting to perform on the tertiary sort column. ///
public ListViewSortType TertiarySortType { get{ return _tertiarySortType; } set{ _tertiarySortType = value; } } #endregion #region IComparer Interface ///
/// Compares two list view items. ///
///
List view item. ///
List view item. ///
An integer representing if the list items are the same or different.
///
0 indicates the values are the same. /// 1 indicates that x is greater than y. /// -1 indicates that x is less than y.
public int Compare(object x, object y) { int result; if ( !_enabled || _primarySortOrder == SortOrder.None ) { // Sorting is disabled return 0; } // Cast items to list view items ListViewItem listItemA = (ListViewItem)x; ListViewItem listItemB = (ListViewItem)y; // Do primary sort result = _Compare( _GetItemText( listItemA, _primaryIndex ), _GetItemText( listItemB, _primaryIndex ), _primarySortType, _primarySortOrder); if( result == 0 && _secondarySortOrder != SortOrder.None ) { // Items are the same, do secondary compare result = _Compare( _GetItemText( listItemA, _secondaryIndex ), _GetItemText( listItemB, _secondaryIndex ), _secondarySortType, _secondarySortOrder); if( result == 0 && _tertiarySortOrder != SortOrder.None ) { // Items are the same, do final comparison result = _Compare( _GetItemText( listItemA, _tertiaryIndex ), _GetItemText( listItemB, _tertiaryIndex ), _tertiarySortType, _tertiarySortOrder); } } return result; } #endregion #region Private Functions ///
/// Returns the list item text. ///
///
The list item. ///
A zero based index of the column to return (-1 indicates return Tag.ToString()). ///
private string _GetItemText( ListViewItem item, int column) { string itemText = string.Empty; if ( column == 0 ) { itemText = item.Text; } else if ( column == -1 ) { if ( item.Tag != null ) { itemText = item.Tag.ToString(); } } else { if ( column < item.SubItems.Count ) { itemText = item.SubItems[column].Text; } } return itemText; } ///
/// Compares two list items. ///
///
///
///
///
///
private int _Compare(string a, string b, ListViewSortType sortType, SortOrder sortOrder) { int retValue = 0; // Compare the list view items in the specified column switch( sortType ) { case ListViewSortType.SortString: // String sort retValue = String.Compare(a, b); break; case ListViewSortType.SortFloat: // Float sort double valx = 0d, valy = 0d; if ( a != string.Empty ) { valx = Double.Parse( a ); } if ( b != string.Empty ) { valy = Double.Parse( b ); } if ( valx < valy ) { retValue = -1; } else if( valx > valy ) { retValue = 1; } retValue = 0; break; case ListViewSortType.SortInt32: // Integer sort int valxi = 0, valyi = 0; if ( a != string.Empty ) { valxi = Int32.Parse( a ); } if ( b != string.Empty ) { valyi = Int32.Parse( b ); } if ( valxi < valyi ) { retValue = -1; } else if( valxi > valyi ) { retValue = 1; } else { retValue = 0; } break; case ListViewSortType.SortDate: // Date sort DateTime valxd = DateTime.MinValue, valyd = DateTime.MinValue; if ( a != string.Empty ) { valxd = DateTime.Parse( a ); } if ( b != string.Empty ) { valyd = DateTime.Parse( b ); } retValue = DateTime.Compare( valxd, valyd); break; default: throw new ArgumentOutOfRangeException( "SortType", _primarySortType, "Invalid Sort Type..."); } // Return the value if ( sortOrder == SortOrder.Ascending ) { return retValue; } else { return retValue * -1; } } #endregion #region Constructors ///
/// Default constructor ///
public ListViewColumnSorter() { } #endregion } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet