@@ -14,35 +14,46 @@ export function compare(a: string, z: string) {
1414 let aCode = a . charCodeAt ( i )
1515 let zCode = z . charCodeAt ( i )
1616
17- // Continue if the characters are the same
18- if ( aCode === zCode ) continue
19-
2017 // If both are numbers, compare them as numbers instead of strings.
2118 if ( aCode >= ZERO && aCode <= NINE && zCode >= ZERO && zCode <= NINE ) {
22- let aStart = i
23- let aEnd = i + 1
24- let zStart = i
25- let zEnd = i + 1
26-
27- // Consume the number
28- aCode = a . charCodeAt ( aEnd )
29- while ( aCode >= ZERO && aCode <= NINE ) aCode = a . charCodeAt ( ++ aEnd )
30-
31- // Consume the number
32- zCode = z . charCodeAt ( zEnd )
33- while ( zCode >= ZERO && zCode <= NINE ) zCode = z . charCodeAt ( ++ zEnd )
34-
35- let aNumber = a . slice ( aStart , aEnd )
36- let zNumber = z . slice ( zStart , zEnd )
37-
38- return (
39- Number ( aNumber ) - Number ( zNumber ) ||
40- // Fallback case if numbers are the same but the string representation
41- // is not. Fallback to string sorting. E.g.: `0123` vs `123`
42- ( aNumber < zNumber ? - 1 : 1 )
43- )
19+ let aValue = 0
20+ let zValue = 0
21+ let aEnd = i
22+ let zEnd = i
23+
24+ do {
25+ aValue *= 10
26+ aValue += a . charCodeAt ( aEnd ) - ZERO
27+ aCode = a . charCodeAt ( ++ aEnd )
28+ } while ( aCode >= ZERO && aCode <= NINE )
29+
30+ do {
31+ zValue *= 10
32+ zValue += z . charCodeAt ( zEnd ) - ZERO
33+ zCode = z . charCodeAt ( ++ zEnd )
34+ } while ( zCode >= ZERO && zCode <= NINE )
35+
36+ if ( aValue - zValue ) {
37+ return aValue - zValue
38+ }
39+
40+ let aNumber = a . slice ( i , aEnd )
41+ let zNumber = z . slice ( i , zEnd )
42+
43+ // Fallback case if numbers are the same but the string representation
44+ // is not. Fallback to string sorting. E.g.: `0123` vs `123`
45+ if ( aNumber < zNumber ) {
46+ return - 1
47+ } else if ( aNumber > zNumber ) {
48+ return 1
49+ } else {
50+ continue
51+ }
4452 }
4553
54+ // Continue if the characters are the same
55+ if ( aCode === zCode ) continue
56+
4657 // Otherwise, compare them as strings
4758 return aCode - zCode
4859 }
0 commit comments