enhance: improve NumericSort.Compare performance

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2024-10-29 09:34:38 +08:00
parent 651d313496
commit 489b57858f
No known key found for this signature in database

View file

@ -21,6 +21,10 @@
int loc2 = 0; int loc2 = 0;
bool isDigit1 = char.IsDigit(c1); bool isDigit1 = char.IsDigit(c1);
bool isDigit2 = char.IsDigit(c2);
if (isDigit1 != isDigit2)
return c1.CompareTo(c2);
do do
{ {
tmp1[loc1] = c1; tmp1[loc1] = c1;
@ -33,7 +37,6 @@
break; break;
} while (char.IsDigit(c1) == isDigit1); } while (char.IsDigit(c1) == isDigit1);
bool isDigit2 = char.IsDigit(c2);
do do
{ {
tmp2[loc2] = c2; tmp2[loc2] = c2;
@ -49,7 +52,7 @@
string sub1 = new string(tmp1, 0, loc1); string sub1 = new string(tmp1, 0, loc1);
string sub2 = new string(tmp2, 0, loc2); string sub2 = new string(tmp2, 0, loc2);
int result; int result;
if (isDigit1 && isDigit2) if (isDigit1)
result = loc1 == loc2 ? string.CompareOrdinal(sub1, sub2) : loc1 - loc2; result = loc1 == loc2 ? string.CompareOrdinal(sub1, sub2) : loc1 - loc2;
else else
result = string.CompareOrdinal(sub1, sub2); result = string.CompareOrdinal(sub1, sub2);