Skip to content

Commit 8012e8a

Browse files
committed
Fix comparison of numbers with different magnitudes
1 parent d045aaa commit 8012e8a

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

packages/tailwindcss/src/utils/compare.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ 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) {
2219
let aStart = i
@@ -35,14 +32,24 @@ export function compare(a: string, z: string) {
3532
let aNumber = a.slice(aStart, aEnd)
3633
let zNumber = z.slice(zStart, zEnd)
3734

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-
)
35+
let diff = Number(aNumber) - Number(zNumber)
36+
if (diff) return diff
37+
38+
// Fallback case if numbers are the same but the string representation
39+
// is not. Fallback to string sorting. E.g.: `0123` vs `123`
40+
if (aNumber < zNumber) return -1
41+
if (aNumber > zNumber) return 1
42+
43+
// Continue with the next character otherwise short strings will appear
44+
// after long ones when containing numbers. E.g.:
45+
// - bg-red-500/70
46+
// - bg-red-500
47+
continue
4448
}
4549

50+
// Continue if the characters are the same
51+
if (aCode === zCode) continue
52+
4653
// Otherwise, compare them as strings
4754
return aCode - zCode
4855
}

0 commit comments

Comments
 (0)