@@ -165,10 +165,39 @@ function shouldSortDependenciesLikeNpm(packageJson) {
165165 return true
166166}
167167
168+ /**
169+ * Custom comparator that sorts @ before - when comparing strings.
170+ * This ensures that "package@..." comes before "package-..." in dependencies.
171+ *
172+ * Strategy: Check character by character until we find a difference.
173+ * If the difference is @ vs -, handle specially. Otherwise, delegate to
174+ * the provided comparison function.
175+ */
176+ const compareWithAtBeforeDash = ( a , b , fallbackCompareFn ) => {
177+ const len = Math . min ( a . length , b . length )
178+
179+ for ( let i = 0 ; i < len ; i ++ ) {
180+ const charA = a [ i ]
181+ const charB = b [ i ]
182+
183+ if ( charA === charB ) continue
184+
185+ // If one is @ and the other is -, @ comes first
186+ if ( charA === '@' && charB === '-' ) return - 1
187+ if ( charA === '-' && charB === '@' ) return 1
188+
189+ // For any other difference, use the fallback comparison
190+ return fallbackCompareFn ? fallbackCompareFn ( a , b ) : a < b ? - 1 : 1
191+ }
192+
193+ // If all characters match up to the shorter length, shorter string comes first
194+ return a . length - b . length
195+ }
196+
168197/**
169198 * Sort dependencies alphabetically, detecting package manager to use the
170199 * appropriate comparison. npm uses locale-aware comparison, yarn and pnpm use
171- * simple string comparison
200+ * simple string comparison. In all cases, @ sorts before - for consistent ordering.
172201 */
173202const sortDependencies = onObject ( ( dependencies , packageJson ) => {
174203 // Avoid file access
@@ -178,11 +207,14 @@ const sortDependencies = onObject((dependencies, packageJson) => {
178207
179208 // sort deps like the npm CLI does (via the package @npmcli/package-json)
180209 // https://github.com/npm/package-json/blob/b6465f44c727d6513db6898c7cbe41dd355cebe8/lib/update-dependencies.js#L8-L21
210+ // but with custom handling for @ before -
181211 if ( shouldSortDependenciesLikeNpm ( packageJson ) ) {
182- return sortObjectKeys ( dependencies , ( a , b ) => a . localeCompare ( b , 'en' ) )
212+ return sortObjectKeys ( dependencies , ( a , b ) =>
213+ compareWithAtBeforeDash ( a , b , ( x , y ) => x . localeCompare ( y , 'en' ) ) ,
214+ )
183215 }
184216
185- return sortObjectKeys ( dependencies )
217+ return sortObjectKeys ( dependencies , ( a , b ) => compareWithAtBeforeDash ( a , b ) )
186218} )
187219
188220/**
0 commit comments