[15992] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[15992] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Windows.Forms;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Clients.Hive.JobManager {
|
---|
| 27 | public class ListViewItemComparer : IComparer {
|
---|
| 28 | private int[] cols;
|
---|
| 29 | public SortOrder[] Orders { get; set; }
|
---|
| 30 | private const string dateFormat = "dd.MM.yyyy HH:mm";
|
---|
| 31 |
|
---|
| 32 | public ListViewItemComparer() {
|
---|
| 33 | cols = new []{ 0 };
|
---|
| 34 | Orders = new[] { SortOrder.Ascending };
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public ListViewItemComparer(int[] columns, SortOrder[] orders) {
|
---|
| 38 | cols = columns;
|
---|
| 39 | Orders = orders;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public int Compare(object x, object y) {
|
---|
| 43 | int returnVal;
|
---|
| 44 | bool result;
|
---|
| 45 | ListViewItem listViewItemX, listViewItemY;
|
---|
| 46 | listViewItemX = x as ListViewItem;
|
---|
| 47 | listViewItemY = y as ListViewItem;
|
---|
| 48 |
|
---|
| 49 | if (listViewItemX == null || listViewItemY == null) {
|
---|
| 50 | throw new ArgumentException(string.Format("The ListViewItemComparer expects ListViewItems but received {0} and {1}.",
|
---|
| 51 | x.GetType().ToString(), y.GetType().ToString()));
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | int cmpres = 0;
|
---|
| 55 | for (int i = 0; i < cols.Length && cmpres == 0; i++) {
|
---|
| 56 | string textX = listViewItemX.SubItems[cols[i]].Text;
|
---|
| 57 | string textY = listViewItemY.SubItems[cols[i]].Text;
|
---|
| 58 |
|
---|
| 59 | DateTime dateX, dateY;
|
---|
| 60 | int intX, intY;
|
---|
| 61 | double doubleX, doubleY;
|
---|
| 62 |
|
---|
| 63 | if(DateTime.TryParse(textX, out dateX) && DateTime.TryParse(textY, out dateY)) {
|
---|
| 64 | cmpres = DateTime.Compare(dateX, dateY);
|
---|
| 65 | } else if(Int32.TryParse(textX, out intX) && Int32.TryParse(textY, out intY)) {
|
---|
| 66 | cmpres = (intX == intY) ? 0 : (intX > intY) ? 1 : -1;
|
---|
| 67 | } else if(Double.TryParse(textX, out doubleX) && Double.TryParse(textY, out doubleY)) {
|
---|
| 68 | cmpres = (doubleX == doubleY) ? 0 : (doubleX > doubleY) ? 1 : -1;
|
---|
| 69 | } else {
|
---|
| 70 | cmpres = String.Compare(textX, textY);
|
---|
| 71 | }
|
---|
| 72 | if(Orders[i] == SortOrder.Descending) {
|
---|
| 73 | cmpres *= -1;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | return cmpres;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|