mirror of
https://fastgit.cc/github.com/sourcegit-scm/sourcegit
synced 2026-04-22 13:51:15 +08:00
enhance: make NumericSort use InvariantCulture instead of Ordinal comparison (#1724)
Ordinal comparison sorts non-alphanumeric chars (like underscore etc) differently than expected (i.e compared to filesystem sorting etc). InvariantCulture comparison corrects this, without imposing cultural differences. (A possible alternative would be CurrentCulture comparison.) The earlier code, when comparing a single digit-char against non-digit-char, used char.CompareTo() where the comparison method can not be specified. To fix this, we now use the same Comparer for all non-all-digits string comparisons.
This commit is contained in:
@@ -6,6 +6,8 @@ namespace SourceGit.Models
|
||||
{
|
||||
public static int Compare(string s1, string s2)
|
||||
{
|
||||
var comparer = StringComparer.InvariantCultureIgnoreCase;
|
||||
|
||||
int len1 = s1.Length;
|
||||
int len2 = s2.Length;
|
||||
|
||||
@@ -20,7 +22,7 @@ namespace SourceGit.Models
|
||||
bool isDigit1 = char.IsDigit(c1);
|
||||
bool isDigit2 = char.IsDigit(c2);
|
||||
if (isDigit1 != isDigit2)
|
||||
return c1.CompareTo(c2);
|
||||
return comparer.Compare(c1.ToString(), c2.ToString());
|
||||
|
||||
int subLen1 = 1;
|
||||
while (marker1 + subLen1 < len1 && char.IsDigit(s1[marker1 + subLen1]) == isDigit1)
|
||||
@@ -40,7 +42,7 @@ namespace SourceGit.Models
|
||||
if (isDigit1)
|
||||
result = (subLen1 == subLen2) ? string.CompareOrdinal(sub1, sub2) : (subLen1 - subLen2);
|
||||
else
|
||||
result = string.Compare(sub1, sub2, StringComparison.OrdinalIgnoreCase);
|
||||
result = comparer.Compare(sub1, sub2);
|
||||
|
||||
if (result != 0)
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user