Advertisement
ForeverZer0

ListViewColumnSorter

Jun 15th, 2012
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1. using System.Collections;
  2. using System.Text.RegularExpressions;  
  3. using System.Windows.Forms;
  4.  
  5. namespace ARCed.Controls
  6. {
  7.     /// <summary>
  8.     /// This class is an implementation of the 'IComparer' interface.
  9.     /// </summary>
  10.     public class ListViewColumnSorter : IComparer
  11.     {
  12.         /// <summary>
  13.         /// Specifies the column to be sorted
  14.         /// </summary>
  15.         private int ColumnToSort;
  16.         /// <summary>
  17.         /// Specifies the order in which to sort (i.e. 'Ascending').
  18.         /// </summary>
  19.         private SortOrder OrderOfSort;
  20.         /// <summary>
  21.         /// Case insensitive comparer object
  22.         /// </summary>
  23.         //private CaseInsensitiveComparer ObjectCompare;
  24.         private NumberCaseInsensitiveComparer ObjectCompare;
  25.         private ImageTextComparer FirstObjectCompare;
  26.  
  27.         /// <summary>
  28.         /// Class constructor.  Initializes various elements
  29.         /// </summary>
  30.         public ListViewColumnSorter()
  31.         {
  32.             // Initialize the column to '0'
  33.             ColumnToSort = 0;
  34.  
  35.             // Initialize the sort order to 'none'
  36.             //OrderOfSort = SortOrder.None;
  37.             OrderOfSort = SortOrder.Ascending;
  38.  
  39.             // Initialize the CaseInsensitiveComparer object
  40.             ObjectCompare = new NumberCaseInsensitiveComparer();//CaseInsensitiveComparer();
  41.             FirstObjectCompare = new ImageTextComparer();
  42.         }
  43.  
  44.         /// <summary>
  45.         /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
  46.         /// </summary>
  47.         /// <param name="x">First object to be compared</param>
  48.         /// <param name="y">Second object to be compared</param>
  49.         /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
  50.         public int Compare(object x, object y)
  51.         {
  52.             int compareResult;
  53.             ListViewItem listviewX, listviewY;
  54.  
  55.             // Cast the objects to be compared to ListViewItem objects
  56.             listviewX = (ListViewItem)x;
  57.             listviewY = (ListViewItem)y;
  58.  
  59.             if (ColumnToSort == 0)
  60.             {
  61.                 compareResult = FirstObjectCompare.Compare(x,y);
  62.             }
  63.             else
  64.             {
  65.                 // Compare the two items
  66.                 compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,listviewY.SubItems[ColumnToSort].Text);
  67.             }
  68.  
  69.             // Calculate correct return value based on object comparison
  70.             if (OrderOfSort == SortOrder.Ascending)
  71.             {
  72.                 // Ascending sort is selected, return normal result of compare operation
  73.                 return compareResult;
  74.             }
  75.             else if (OrderOfSort == SortOrder.Descending)
  76.             {
  77.                 // Descending sort is selected, return negative result of compare operation
  78.                 return (-compareResult);
  79.             }
  80.             else
  81.             {
  82.                 // Return '0' to indicate they are equal
  83.                 return 0;
  84.             }
  85.         }
  86.    
  87.         /// <summary>
  88.         /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
  89.         /// </summary>
  90.         public int SortColumn
  91.         {
  92.             set
  93.             {
  94.                 ColumnToSort = value;
  95.             }
  96.             get
  97.             {
  98.                 return ColumnToSort;
  99.             }
  100.         }
  101.  
  102.         /// <summary>
  103.         /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
  104.         /// </summary>
  105.         public SortOrder Order
  106.         {
  107.             set
  108.             {
  109.                 OrderOfSort = value;
  110.             }
  111.             get
  112.             {
  113.                 return OrderOfSort;
  114.             }
  115.         }
  116.    
  117.     }
  118.  
  119.     public class ImageTextComparer : IComparer
  120.     {
  121.         //private CaseInsensitiveComparer ObjectCompare;
  122.         private NumberCaseInsensitiveComparer ObjectCompare;
  123.        
  124.         public ImageTextComparer()
  125.         {
  126.             // Initialize the CaseInsensitiveComparer object
  127.             ObjectCompare = new NumberCaseInsensitiveComparer();//CaseInsensitiveComparer();
  128.         }
  129.  
  130.         public int Compare(object x, object y)
  131.         {
  132.             //int compareResult;
  133.             int image1, image2;
  134.             ListViewItem listviewX, listviewY;
  135.  
  136.             // Cast the objects to be compared to ListViewItem objects
  137.             listviewX = (ListViewItem)x;
  138.             image1 = listviewX.ImageIndex;
  139.             listviewY = (ListViewItem)y;
  140.             image2 = listviewY.ImageIndex;
  141.  
  142.             if (image1 < image2)
  143.             {
  144.                 return -1;
  145.             }
  146.             else if (image1 == image2)
  147.             {
  148.                 return ObjectCompare.Compare(listviewX.Text,listviewY.Text);
  149.             }
  150.             else
  151.             {
  152.                 return 1;
  153.             }
  154.         }
  155.     }
  156.  
  157.     public class NumberCaseInsensitiveComparer : CaseInsensitiveComparer
  158.     {
  159.         public NumberCaseInsensitiveComparer ()
  160.         {
  161.            
  162.         }
  163.  
  164.         public new int Compare(object x, object y)
  165.         {
  166.             if ((x is System.String) && IsWholeNumber((string)x) && (y is System.String) && IsWholeNumber((string)y))
  167.             {
  168.                 return base.Compare(System.Convert.ToInt32(x),System.Convert.ToInt32(y));
  169.             }
  170.             else
  171.             {
  172.                 return base.Compare(x,y);
  173.             }
  174.         }
  175.  
  176.         private bool IsWholeNumber(string strNumber)
  177.         {
  178.             Regex objNotWholePattern=new Regex("[^0-9]");
  179.             return !objNotWholePattern.IsMatch(strNumber);
  180.         }  
  181.     }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement