-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathAlphaNumStringComparer.cs
More file actions
34 lines (29 loc) · 899 Bytes
/
AlphaNumStringComparer.cs
File metadata and controls
34 lines (29 loc) · 899 Bytes
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PDTools.Utils;
public class AlphaNumStringComparer : IComparer<string>
{
private static readonly AlphaNumStringComparer _default = new AlphaNumStringComparer();
public static AlphaNumStringComparer Default => _default;
public int Compare(string? value1, string? value2)
{
string v1 = value1 ?? string.Empty;
string v2 = value2 ?? string.Empty;
int min = v1.Length > v2.Length ? v2.Length : v1.Length;
for (int i = 0; i < min; i++)
{
if (v1[i] < v2[i])
return -1;
else if (v1[i] > v2[i])
return 1;
}
if (v1.Length < v2.Length)
return -1;
else if (v1.Length > v2.Length)
return 1;
return 0;
}
}