-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCompare.cs
More file actions
50 lines (47 loc) · 1.38 KB
/
StringCompare.cs
File metadata and controls
50 lines (47 loc) · 1.38 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
namespace DA.Algorithms.Strings
{
public static class StringCompare
{
/// <summary>
/// Compare two strings.
/// </summary>
///
/// <param name="source">first string</param>
/// <param name="other">second string</param>
///
/// <returns>
/// <para> 0 - the both first and second strings are equal. </para>
/// <para> negative value - the first string is less then the second. </para>
/// <para> positive value - the first string is greater than the second. </para>
/// </returns>
public static int Compare (string source, string other)
{
int index = 0;
int minLength = source.Length;
if (source.Length > other.Length)
{
minLength = other.Length;
}
while (index < minLength && source[index] == other[index])
{
++index;
}
if (index == source.Length && index == other.Length)
{
return 0;
}
else if (index == source.Length)
{
return -1;
}
else if (index == other.Length)
{
return 1;
}
else
{
return source[index] - other[index];
}
}
}
}