-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathHashComparer.cs
More file actions
114 lines (100 loc) · 3.01 KB
/
Copy pathHashComparer.cs
File metadata and controls
114 lines (100 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NMController
{
public class HashComparer : IComparer
{
private readonly ListSortDirection direction;
private readonly string sortBy;
public HashComparer(ListSortDirection direction, string sortBy)
{
this.direction = direction;
this.sortBy = sortBy;
}
public int Compare(object? x, object? y)
{
NMDevice devx = x as NMDevice;
NMDevice devy = y as NMDevice;
string strX = "";
string strY = "";
if(sortBy == "LastDiff")
{
strX = devx.LastDiff;
strY = devy.LastDiff;
}
else if(sortBy == "BestDiff")
{
strX = ExtractBestDiff(devx.BestDiff);
strY = ExtractBestDiff(devy.BestDiff);
}
else
{
strX = devx.NetDiff;
strY = devy.NetDiff;
}
if (strX == null || strY == null)
{
return 0;
}
double valueX = ParseValue(strX);
double valueY = ParseValue(strY);
int result = valueX.CompareTo(valueY);
return direction == ListSortDirection.Ascending ? result : -result;
}
private string ExtractBestDiff(string str)
{
var parts = str.Split(new[] { "\r" }, StringSplitOptions.None);
if (parts.Length > 1)
{
return parts[1].Trim();
}
return "0.000";
}
private double ParseValue(string str)
{
if (string.IsNullOrEmpty(str))
{
return 0;
}
char lastChar = str[str.Length - 1];
string numberPart = str.Substring(0, str.Length - 1);
double multiplier = 1;
switch (lastChar)
{
case 'E':
case 'e':
multiplier = 1e18;
break;
case 'P':
case 'p':
multiplier = 1e15;
break;
case 'T':
case 't':
multiplier = 1e12;
break;
case 'G':
case 'g':
multiplier = 1e9;
break;
case 'M':
case 'm':
multiplier = 1e6;
break;
case 'K':
case 'k':
multiplier = 1e3;
break;
default:
return double.Parse(numberPart, CultureInfo.InvariantCulture);
}
return double.Parse(numberPart, CultureInfo.InvariantCulture) * multiplier;
}
}
}