Skip to content

Commit 489b578

Browse files
committed
enhance: improve NumericSort.Compare performance
Signed-off-by: leo <[email protected]>
1 parent 651d313 commit 489b578

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/Models/NumericSort.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public static int Compare(string s1, string s2)
2121
int loc2 = 0;
2222

2323
bool isDigit1 = char.IsDigit(c1);
24+
bool isDigit2 = char.IsDigit(c2);
25+
if (isDigit1 != isDigit2)
26+
return c1.CompareTo(c2);
27+
2428
do
2529
{
2630
tmp1[loc1] = c1;
@@ -33,7 +37,6 @@ public static int Compare(string s1, string s2)
3337
break;
3438
} while (char.IsDigit(c1) == isDigit1);
3539

36-
bool isDigit2 = char.IsDigit(c2);
3740
do
3841
{
3942
tmp2[loc2] = c2;
@@ -49,7 +52,7 @@ public static int Compare(string s1, string s2)
4952
string sub1 = new string(tmp1, 0, loc1);
5053
string sub2 = new string(tmp2, 0, loc2);
5154
int result;
52-
if (isDigit1 && isDigit2)
55+
if (isDigit1)
5356
result = loc1 == loc2 ? string.CompareOrdinal(sub1, sub2) : loc1 - loc2;
5457
else
5558
result = string.CompareOrdinal(sub1, sub2);

0 commit comments

Comments
 (0)