Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Clients.Hive.JobManager/3.3/ListViewItemDateComparer.cs @ 11997

Last change on this file since 11997 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 2.6 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 ListViewItemDateComparer : IComparer {
32    private int col;
33    private SortOrder order;
34
35    public ListViewItemDateComparer() {
36      col = 0;
37      order = SortOrder.Ascending;
38    }
39
40    public ListViewItemDateComparer(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      bool result;
48      DateTime firstDate, secondDate;
49      ListViewItem listViewItemX, listViewItemY;
50      listViewItemX = x as ListViewItem;
51      listViewItemY = y as ListViewItem;
52
53      if (listViewItemX == null || listViewItemY == null) {
54        throw new ArgumentException(string.Format("The ListViewItemDateComparer expects ListViewItems but received {0} and {1}.",
55          x.GetType().ToString(), y.GetType().ToString()));
56      }
57
58      result = DateTime.TryParse(listViewItemX.SubItems[col].Text, out firstDate);
59      result = DateTime.TryParse(listViewItemY.SubItems[col].Text, out secondDate) && result;
60
61      if (result) {
62        returnVal = DateTime.Compare(firstDate, secondDate);
63      } else {
64        throw new ArgumentException(string.Format("The ListViewItemDateComparer expects DateTimes. Can't parse {0} and {1}.",
65           listViewItemX.SubItems[col].Text, listViewItemY.SubItems[col].Text));
66      }
67
68      if (order == SortOrder.Descending) {
69        // invert the value returned by Compare.
70        returnVal *= -1;
71      }
72      return returnVal;
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.