-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathHashRateComparer.cs
More file actions
85 lines (74 loc) · 2.24 KB
/
Copy pathHashRateComparer.cs
File metadata and controls
85 lines (74 loc) · 2.24 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
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 HashRateComparer : IComparer
{
private readonly ListSortDirection direction;
public HashRateComparer(ListSortDirection direction)
{
this.direction = direction;
}
public int Compare(object? x, object? y)
{
NMDevice devx = x as NMDevice;
NMDevice devy = y as NMDevice;
string strX = devx.HashRate;
string strY = devy.HashRate;
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 double ParseValue(string str)
{
if (string.IsNullOrEmpty(str))
{
return 0;
}
char lastChar = str[str.Length - 4];
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(str, CultureInfo.InvariantCulture);
}
string numberPart = str.Substring(0, str.Length - 4);
return double.Parse(numberPart, CultureInfo.InvariantCulture) * multiplier;
}
}
}