diff --git a/isort/output.py b/isort/output.py index 6b3401089..ee7406ca8 100644 --- a/isort/output.py +++ b/isort/output.py @@ -54,12 +54,15 @@ def sorted_imports( key=lambda key: sorting.module_key( key, config, section_name=section, straight_import=True ), + reverse=config.reverse_sort, ) from_modules = parsed.imports[section]["from"] if not config.only_sections: from_modules = sorting.naturally( - from_modules, key=lambda key: sorting.module_key(key, config, section_name=section) + from_modules, + key=lambda key: sorting.module_key(key, config, section_name=section), + reverse=config.reverse_sort, ) straight_imports = _with_straight_imports( @@ -233,6 +236,7 @@ def _with_from_imports( config.force_alphabetical_sort_within_sections, section_name=section, ), + reverse=config.reverse_sort, ) if remove_imports: from_imports = [ diff --git a/isort/sorting.py b/isort/sorting.py index 2cfe60bf5..e8c355bab 100644 --- a/isort/sorting.py +++ b/isort/sorting.py @@ -96,7 +96,9 @@ def section_key(line: str, config: Config) -> str: return f"{section}{len(line) if config.length_sort else ''}{line}" -def naturally(to_sort: Iterable[str], key: Optional[Callable[[str], Any]] = None) -> List[str]: +def naturally( + to_sort: Iterable[str], key: Optional[Callable[[str], Any]] = None, reverse: bool = False +) -> List[str]: """Returns a naturally sorted list""" if key is None: key_callback = _natural_keys @@ -105,7 +107,7 @@ def naturally(to_sort: Iterable[str], key: Optional[Callable[[str], Any]] = None def key_callback(text: str) -> List[Any]: return _natural_keys(key(text)) # type: ignore - return sorted(to_sort, key=key_callback) + return sorted(to_sort, key=key_callback, reverse=reverse) def _atoi(text: str) -> Any: