From 489b57858fea952d6afa083c023e5db1f676484f Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 29 Oct 2024 09:34:38 +0800 Subject: [PATCH] enhance: improve `NumericSort.Compare` performance Signed-off-by: leo --- src/Models/NumericSort.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Models/NumericSort.cs b/src/Models/NumericSort.cs index 58e757b6..ed5002e6 100644 --- a/src/Models/NumericSort.cs +++ b/src/Models/NumericSort.cs @@ -21,6 +21,10 @@ int loc2 = 0; bool isDigit1 = char.IsDigit(c1); + bool isDigit2 = char.IsDigit(c2); + if (isDigit1 != isDigit2) + return c1.CompareTo(c2); + do { tmp1[loc1] = c1; @@ -33,7 +37,6 @@ break; } while (char.IsDigit(c1) == isDigit1); - bool isDigit2 = char.IsDigit(c2); do { tmp2[loc2] = c2; @@ -49,7 +52,7 @@ string sub1 = new string(tmp1, 0, loc1); string sub2 = new string(tmp2, 0, loc2); int result; - if (isDigit1 && isDigit2) + if (isDigit1) result = loc1 == loc2 ? string.CompareOrdinal(sub1, sub2) : loc1 - loc2; else result = string.CompareOrdinal(sub1, sub2);