@@ -626,14 +626,21 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
626626 const collateCaseSensitive = getOrganizeImportsComparer ( preferences , /*ignoreCase*/ false ) ;
627627 const collateCaseInsensitive = getOrganizeImportsComparer ( preferences , /*ignoreCase*/ true ) ;
628628 let sortState = SortKind . Both ;
629+ let seenUnsortedGroup = false ;
629630 for ( const importGroup of importGroups ) {
630631 // Check module specifiers
631632 if ( importGroup . length > 1 ) {
632- sortState & = detectSortCaseSensitivity (
633+ const moduleSpecifierSort = detectSortCaseSensitivity (
633634 importGroup ,
634635 i => tryCast ( i . moduleSpecifier , isStringLiteral ) ?. text ?? "" ,
635636 collateCaseSensitive ,
636637 collateCaseInsensitive ) ;
638+ if ( moduleSpecifierSort ) {
639+ // Don't let a single unsorted group of module specifiers make the whole algorithm detect unsorted.
640+ // If other things are sorted consistently, that's a stronger indicator than unsorted module specifiers.
641+ sortState &= moduleSpecifierSort ;
642+ seenUnsortedGroup = true ;
643+ }
637644 if ( ! sortState ) {
638645 return sortState ;
639646 }
@@ -644,7 +651,13 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
644651 importGroup ,
645652 i => tryCast ( i . importClause ?. namedBindings , isNamedImports ) ?. elements . length ! > 1 ) ;
646653 if ( declarationWithNamedImports ) {
647- sortState &= detectImportSpecifierSorting ( ( declarationWithNamedImports . importClause ! . namedBindings as NamedImports ) . elements , preferences ) ;
654+ const namedImportSort = detectImportSpecifierSorting ( ( declarationWithNamedImports . importClause ! . namedBindings as NamedImports ) . elements , preferences ) ;
655+ if ( namedImportSort ) {
656+ // Don't let a single unsorted group of named imports make the whole algorithm detect unsorted.
657+ // If other things are sorted consistently, that's a stronger indicator than unsorted named imports.
658+ sortState &= namedImportSort ;
659+ seenUnsortedGroup = true ;
660+ }
648661 if ( ! sortState ) {
649662 return sortState ;
650663 }
@@ -657,7 +670,7 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
657670 return sortState ;
658671 }
659672 }
660- return sortState ;
673+ return seenUnsortedGroup ? SortKind . None : sortState ;
661674}
662675
663676/** @internal */
0 commit comments