summaryrefslogtreecommitdiff
path: root/natsort/utils.py
diff options
context:
space:
mode:
authorSeth Morton <seth.m.morton@gmail.com>2023-02-26 23:21:37 -0800
committerSeth Morton <seth.m.morton@gmail.com>2023-02-26 23:47:50 -0800
commitcf2a55daf7e74d177c95149da623172b1b6d93ae (patch)
tree6db55e4ddf210e4c7fe4824079ecc16bcd8abc62 /natsort/utils.py
parente778c1742fc94766b42110580809795605ca3c88 (diff)
downloadnatsort-cf2a55daf7e74d177c95149da623172b1b6d93ae.tar.gz
Ensure None, NaN, and Infinity are sorted consistently
Internally, these may be translated to the same value, so they will be output in the same order they were input, which could lead to suprise. This commit ensures the order is always consistent.
Diffstat (limited to 'natsort/utils.py')
-rw-r--r--natsort/utils.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/natsort/utils.py b/natsort/utils.py
index b86225e..2a3745f 100644
--- a/natsort/utils.py
+++ b/natsort/utils.py
@@ -420,7 +420,17 @@ def parse_number_or_none_factory(
val: Any, _nan_replace: float = nan_replace, _sep: StrOrBytes = sep
) -> BasicTuple:
"""Given a number, place it in a tuple with a leading null string."""
- return _sep, (_nan_replace if val != val or val is None else val)
+ # Add a trailing string numbers equaling _nan_replace. This will make
+ # the ordering between None NaN, and the NaN replacement value...
+ # None comes first, then NaN, then the replacement value.
+ if val is None:
+ return _sep, _nan_replace, "1"
+ elif val != val:
+ return _sep, _nan_replace, "2"
+ elif val == _nan_replace:
+ return _sep, _nan_replace, "3"
+ else:
+ return _sep, val
# Return the function, possibly wrapping in tuple if PATH is selected.
if alg & ns.PATH and alg & ns.UNGROUPLETTERS and alg & ns.LOCALEALPHA: