Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive.JobManager/3.3/ListViewDateComparer.cs @ 8702

Last change on this file since 8702 was 8702, checked in by ascheibe, 12 years ago

#1959 added a comparer for sorting the jobs in the Hive Job Manager by date

File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Windows.Forms;
25
26namespace HeuristicLab.Clients.Hive.JobManager {
27  /// <summary>
28  /// Comparer for sorting items in a list view by date
29  /// See: http://msdn.microsoft.com/en-us/library/ms996467.aspx
30  /// </summary>
31  public class ListViewDateComparer : IComparer {
32    private int col;
33    private SortOrder order;
34
35    public ListViewDateComparer() {
36      col = 0;
37      order = SortOrder.Ascending;
38    }
39
40    public ListViewDateComparer(int column, SortOrder order) {
41      col = column;
42      this.order = order;
43    }
44
45    public int Compare(object x, object y) {
46      int returnVal;
47
48      if (!(x is ListViewItem) || !(y is ListViewItem)) {
49        throw new InvalidCastException(string.Format("The ListViewDateComparer expects ListViewItems but received {0} and {1}.",
50          x.GetType().ToString(), y.GetType().ToString()));
51      }
52
53      try {
54        DateTime firstDate = DateTime.Parse(((ListViewItem)x).SubItems[col].Text);
55        DateTime secondDate = DateTime.Parse(((ListViewItem)y).SubItems[col].Text);
56        returnVal = DateTime.Compare(firstDate, secondDate);
57      }
58      // if neither compared object has a valid date format, compare as a string
59      catch {
60        returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
61      }
62
63      if (order == SortOrder.Descending) {
64        // invert the value returned by Compare.
65        returnVal *= -1;
66      }
67      return returnVal;
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.