Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3107_LearningALPS/HeuristicLab.Clients.Hive.JobManager/3.3/ListViewItemDateComparer.cs @ 18132

Last change on this file since 18132 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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    public SortOrder Order { get; set; }
34
35    public ListViewItemDateComparer() {
36      col = 0;
37      Order = SortOrder.Ascending;
38    }
39
40    public ListViewItemDateComparer(int column, SortOrder order) {
41      col = column;
42      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.