ListViewColumnSorter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Threading.Tasks;
  8. namespace Optimizer
  9. {
  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. /// <summary>
  25. /// Initializes a new instance of the ListViewColumnSorter class
  26. /// </summary>
  27. public ListViewColumnSorter()
  28. {
  29. // Initialize the column to '0'
  30. this.columnToSort = 0;
  31. // Initialize the sort order to 'none'
  32. this.orderOfSort = SortOrder.None;
  33. // Initialize the CaseInsensitiveComparer object
  34. this.objectCompare = new CaseInsensitiveComparer();
  35. }
  36. /// <summary>
  37. /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
  38. /// </summary>
  39. public int SortColumn
  40. {
  41. get
  42. {
  43. return this.columnToSort;
  44. }
  45. set
  46. {
  47. this.columnToSort = value;
  48. }
  49. }
  50. /// <summary>
  51. /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
  52. /// </summary>
  53. public SortOrder Order
  54. {
  55. get
  56. {
  57. return this.orderOfSort;
  58. }
  59. set
  60. {
  61. this.orderOfSort = value;
  62. }
  63. }
  64. /// <summary>
  65. /// This method is inherited from the IComparer interface. It compares the two objects passed using a case insensitive comparison.
  66. /// </summary>
  67. /// <param name="x">First object to be compared</param>
  68. /// <param name="y">Second object to be compared</param>
  69. /// <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>
  70. public int Compare(object x, object y)
  71. {
  72. int compareResult;
  73. ListViewItem listviewX, listviewY;
  74. // Cast the objects to be compared to ListViewItem objects
  75. listviewX = (ListViewItem)x;
  76. listviewY = (ListViewItem)y;
  77. // Compare the two items
  78. try
  79. {
  80. // if the numeric sort (try) fails, then we want
  81. // to do the text sort (catch)
  82. compareResult = this.objectCompare.Compare(Convert.ToInt64(listviewX.SubItems[this.columnToSort].Text), Convert.ToInt64(listviewY.SubItems[this.columnToSort].Text));
  83. }
  84. catch
  85. {
  86. compareResult = this.objectCompare.Compare(listviewX.SubItems[this.columnToSort].Text, listviewY.SubItems[this.columnToSort].Text);
  87. }
  88. // Calculate correct return value based on object comparison
  89. if (this.orderOfSort == SortOrder.Ascending)
  90. {
  91. // Ascending sort is selected, return normal result of compare operation
  92. return compareResult;
  93. }
  94. else if (this.orderOfSort == SortOrder.Descending)
  95. {
  96. // Descending sort is selected, return negative result of compare operation
  97. return -compareResult;
  98. }
  99. else
  100. {
  101. // Return '0' to indicate they are equal
  102. return 0;
  103. }
  104. }
  105. }
  106. }