Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.Console/3.2/ListViewColumnSorterDate.cs @ 1812

Last change on this file since 1812 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 3.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Forms;
6using System.Collections;
7
8
9namespace HeuristicLab.Hive.Client.Console {
10  public class ListViewColumnSorterDate : 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
25    /// <summary>
26    /// Class constructor.  Initializes various elements
27    /// </summary>
28    public ListViewColumnSorterDate() {
29      // Initialize the column to '0'
30      ColumnToSort = 0;
31
32      // Initialize the sort order to 'none'
33      OrderOfSort = SortOrder.None;
34
35      // Initialize the CaseInsensitiveComparer object
36      ObjectCompare = new CaseInsensitiveComparer();
37    }
38
39    /// <summary>
40    /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
41    /// </summary>
42    /// <param name="x">First object to be compared</param>
43    /// <param name="y">Second object to be compared</param>
44    /// <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>
45    public int Compare(object x, object y) {
46      int compareResult = 0;
47      ListViewItem listviewX, listviewY;
48
49      // Cast the objects to be compared to ListViewItem objects
50      listviewX = (ListViewItem)x;
51      listviewY = (ListViewItem)y;
52
53      DateTime listviewDateX = new DateTime();
54      DateTime listviewDateY = new DateTime();
55
56      if (DateTime.TryParse(listviewX.SubItems[ColumnToSort].Text, out listviewDateX) && DateTime.TryParse(listviewY.SubItems[ColumnToSort].Text, out listviewDateY)) {
57        compareResult = DateTime.Compare(listviewDateX, listviewDateY);
58      } else {
59        // Compare the two items
60        compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
61      }
62      // Calculate correct return value based on object comparison
63      if (OrderOfSort == SortOrder.Ascending) {
64        // Ascending sort is selected, return normal result of compare operation
65        return compareResult;
66      } else if (OrderOfSort == SortOrder.Descending) {
67        // Descending sort is selected, return negative result of compare operation
68        return (-compareResult);
69      } else {
70        // Return '0' to indicate they are equal
71        return 0;
72      }
73    }
74
75    /// <summary>
76    /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
77    /// </summary>
78    public int SortColumn {
79      set {
80        ColumnToSort = value;
81      }
82      get {
83        return ColumnToSort;
84      }
85    }
86
87    /// <summary>
88    /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
89    /// </summary>
90    public SortOrder Order {
91      set {
92        OrderOfSort = value;
93      }
94      get {
95        return OrderOfSort;
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.