Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Unreleased

### Fixed

- Recognize locale thousands separators when `COMPATIBILITYNORMALIZE` is enabled,
including non-breaking spaces used by French locales.
- CLI no longer strips leading and trailing whitespace from entries, which
could change the sort order
([@baldassarreFe](https://github.com/baldassarreFe), issue
Expand Down
4 changes: 3 additions & 1 deletion natsort/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,10 @@ def input_string_transform_factory(alg: NSType) -> StrToStr:
nodecimal += r"(?<!" + d + r"[0-9])"
nodecimal += r"(?<!" + d + r"[0-9]{2})"
nodecimal += r"(?<!" + d + r"[0-9]{3})"
# Input is already normalized when this expression is applied.
thousands_sep = _normalize_input_factory(alg)(get_thousands_sep())
strip_thousands = strip_thousands.format(
thou=re.escape(get_thousands_sep()),
thou=re.escape(thousands_sep),
nodecimal=nodecimal,
)
strip_thousands_re = re.compile(strip_thousands, flags=re.VERBOSE)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_natsorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ def test_natsorted_numbers_in_ascending_order() -> None:
assert natsorted(given) == expected


@pytest.mark.parametrize("separator", ["\u00a0", "\u202f"])
@pytest.mark.parametrize("normalize", [ns.DEFAULT, ns.COMPATIBILITYNORMALIZE])
@pytest.mark.parametrize("number_type", [ns.INT, ns.FLOAT])
def test_natsorted_normalizes_locale_thousands_separator(
separator: str,
normalize: NSType,
number_type: NSType,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr("natsort.utils.get_thousands_sep", lambda: separator)
monkeypatch.setattr("natsort.utils.get_decimal_point", lambda: ",")
if number_type == ns.FLOAT:
expected = ["999,9", f"1{separator}000,05", f"1{separator}000,5"]
else:
expected = ["999", f"1{separator}000", f"1{separator}001"]

assert (
natsorted(reversed(expected), alg=ns.LOCALENUM | normalize | number_type)
== expected
)


def test_natsorted_can_sort_as_signed_floats_with_exponents(
float_list: list[str],
) -> None:
Expand Down
Loading