From da98d2d81eb6fc6e9ae28790c3642d5cd6c3a5bb Mon Sep 17 00:00:00 2001 From: Lukas Kleybolte Date: Wed, 14 Oct 2020 15:57:39 +0200 Subject: [PATCH 1/8] add natural_sort.py --- sorts/natural_sort.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 sorts/natural_sort.py diff --git a/sorts/natural_sort.py b/sorts/natural_sort.py new file mode 100644 index 000000000000..50a896489c58 --- /dev/null +++ b/sorts/natural_sort.py @@ -0,0 +1,34 @@ +from __future__ import annotations +import re + + +def natural_sort(input_list: list[str]) -> list[str]: + """ + Sort the given list of strings in the way that humans expect. + + The normal python sort algorithm sorts lexicographically, + so you might not get the results that you expect + >>> example1 = ['2 ft 7 in', '1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '7 ft 6 in'] + >>> example2 = ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] + >>> sorted(example1) + ['1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '2 ft 7 in', '7 ft 6 in'] + + >>> sorted(example2) + ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] + + The natural sort algorith sort based on meaning and not computer code point. + >>> natural_sort(example1) + ['1 ft 5 in', '2 ft 7 in', '2 ft 11 in', '7 ft 6 in', '10 ft 2 in'] + + >>> natural_sort(example2) + ['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13'] + + """ + + def alphanum_key(key: str) -> list: + return [ + int(text) if text.isdigit() else text.lower() + for text in re.split("([0-9]+)", key) + ] + + return sorted(input_list, key=alphanum_key) From 2911a3fb450e0d80c3e314bc15421781ba2b6b1f Mon Sep 17 00:00:00 2001 From: Lukas Kleybolte Date: Wed, 14 Oct 2020 16:07:58 +0200 Subject: [PATCH 2/8] fix doctest --- sorts/natural_sort.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sorts/natural_sort.py b/sorts/natural_sort.py index 50a896489c58..d96137efbd72 100644 --- a/sorts/natural_sort.py +++ b/sorts/natural_sort.py @@ -25,10 +25,16 @@ def natural_sort(input_list: list[str]) -> list[str]: """ - def alphanum_key(key: str) -> list: + def alphanum_key(key): return [ int(text) if text.isdigit() else text.lower() for text in re.split("([0-9]+)", key) ] return sorted(input_list, key=alphanum_key) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 205b020d79b01f0bf9457452ba6103394a483099 Mon Sep 17 00:00:00 2001 From: Lukas Kleybolte Date: Wed, 14 Oct 2020 16:22:22 +0200 Subject: [PATCH 3/8] add 're' to requirements.txt and fix spelling errors --- requirements.txt | 1 + sorts/natural_sort.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 67d9bbbd8448..145cf24a8a60 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,3 +14,4 @@ sklearn sympy tensorflow xgboost +re diff --git a/sorts/natural_sort.py b/sorts/natural_sort.py index d96137efbd72..5988391e9813 100644 --- a/sorts/natural_sort.py +++ b/sorts/natural_sort.py @@ -16,7 +16,7 @@ def natural_sort(input_list: list[str]) -> list[str]: >>> sorted(example2) ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] - The natural sort algorith sort based on meaning and not computer code point. + The natural sort algorithm sort based on meaning and not computer code point. >>> natural_sort(example1) ['1 ft 5 in', '2 ft 7 in', '2 ft 11 in', '7 ft 6 in', '10 ft 2 in'] From 5568cd6f35d740062f03ba817731834b02dce6f1 Mon Sep 17 00:00:00 2001 From: Lukas Kleybolte Date: Wed, 14 Oct 2020 16:39:41 +0200 Subject: [PATCH 4/8] delete 're' from requirements.txt --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 145cf24a8a60..67d9bbbd8448 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,4 +14,3 @@ sklearn sympy tensorflow xgboost -re From 8344fac1b003f0f59263ee6ada8b039849ebb8ae Mon Sep 17 00:00:00 2001 From: Lukas Kleybolte Date: Wed, 14 Oct 2020 16:55:13 +0200 Subject: [PATCH 5/8] fixing linting errors --- sorts/natural_sort.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorts/natural_sort.py b/sorts/natural_sort.py index 5988391e9813..87f9f5e45ad5 100644 --- a/sorts/natural_sort.py +++ b/sorts/natural_sort.py @@ -1,4 +1,5 @@ from __future__ import annotations + import re From d0f081d62f22df2c9f79caf9d9469afedc3b3e94 Mon Sep 17 00:00:00 2001 From: Mozartus <32893711+Mozartuss@users.noreply.github.com> Date: Thu, 15 Oct 2020 12:55:39 +0200 Subject: [PATCH 6/8] Update sorts/natural_sort.py Co-authored-by: Christian Clauss --- sorts/natural_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/natural_sort.py b/sorts/natural_sort.py index 87f9f5e45ad5..53a77e842220 100644 --- a/sorts/natural_sort.py +++ b/sorts/natural_sort.py @@ -7,7 +7,7 @@ def natural_sort(input_list: list[str]) -> list[str]: """ Sort the given list of strings in the way that humans expect. - The normal python sort algorithm sorts lexicographically, + The normal Python sort algorithm sorts lexicographically, so you might not get the results that you expect >>> example1 = ['2 ft 7 in', '1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '7 ft 6 in'] >>> example2 = ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] From ad346f2fdbd97b2e3faf5d406e105055f79a2766 Mon Sep 17 00:00:00 2001 From: Mozartus <32893711+Mozartuss@users.noreply.github.com> Date: Thu, 15 Oct 2020 12:55:48 +0200 Subject: [PATCH 7/8] Update sorts/natural_sort.py Co-authored-by: Christian Clauss --- sorts/natural_sort.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/sorts/natural_sort.py b/sorts/natural_sort.py index 53a77e842220..0ec8778d2a51 100644 --- a/sorts/natural_sort.py +++ b/sorts/natural_sort.py @@ -10,17 +10,15 @@ def natural_sort(input_list: list[str]) -> list[str]: The normal Python sort algorithm sorts lexicographically, so you might not get the results that you expect >>> example1 = ['2 ft 7 in', '1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '7 ft 6 in'] - >>> example2 = ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] >>> sorted(example1) ['1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '2 ft 7 in', '7 ft 6 in'] - - >>> sorted(example2) - ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] - - The natural sort algorithm sort based on meaning and not computer code point. + >>> # The natural sort algorithm sort based on meaning and not computer code point. >>> natural_sort(example1) ['1 ft 5 in', '2 ft 7 in', '2 ft 11 in', '7 ft 6 in', '10 ft 2 in'] + >>> example2 = ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] + >>> sorted(example2) + ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] >>> natural_sort(example2) ['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13'] From 18e778ec9754a7b51dc5018f253ee7783abf911e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 15 Oct 2020 13:31:06 +0200 Subject: [PATCH 8/8] Update natural_sort.py --- sorts/natural_sort.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sorts/natural_sort.py b/sorts/natural_sort.py index 0ec8778d2a51..001ff2cf5b41 100644 --- a/sorts/natural_sort.py +++ b/sorts/natural_sort.py @@ -8,7 +8,8 @@ def natural_sort(input_list: list[str]) -> list[str]: Sort the given list of strings in the way that humans expect. The normal Python sort algorithm sorts lexicographically, - so you might not get the results that you expect + so you might not get the results that you expect... + >>> example1 = ['2 ft 7 in', '1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '7 ft 6 in'] >>> sorted(example1) ['1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '2 ft 7 in', '7 ft 6 in'] @@ -21,14 +22,10 @@ def natural_sort(input_list: list[str]) -> list[str]: ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] >>> natural_sort(example2) ['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13'] - """ def alphanum_key(key): - return [ - int(text) if text.isdigit() else text.lower() - for text in re.split("([0-9]+)", key) - ] + return [int(s) if s.isdigit() else s.lower() for s in re.split("([0-9]+)", key)] return sorted(input_list, key=alphanum_key)