Skip to content

Commit 4c79ac5

Browse files
committed
Fixed string.sorted_natural
1 parent 67eec37 commit 4c79ac5

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/tinyscript/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.30.24
1+
1.30.25

src/tinyscript/preimports/stringp.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,39 @@
44
"""
55
import re
66
import string
7+
from functools import wraps
78

89
from ..helpers.termsize import get_terminal_size
910

1011

11-
def _natural_key(text):
12+
def _natural_key(case_sensitive=False):
1213
""" Key computation function for considering keys in a natural way.
1314
1415
:param text: text to be used for computing the key
1516
"""
16-
tokens = []
17-
for s in re.split(r"(\d+|\D+)", text):
18-
tokens.append(int(s) if s.isdigit() else s.lower())
19-
return tokens
17+
def _wrapper(text):
18+
return [(0, int(t)) if t.isdigit() else (1, t if case_sensitive else t.lower()) for t in \
19+
re.split(r"(\d+|\D+)", text)]
20+
return _wrapper
2021
string.natural_key = _natural_key
2122

2223

23-
def sort_natural(strings):
24+
def sort_natural(strings, case_sensitive=False):
2425
""" Simple function to sort a list of strings with numbers inside.
2526
2627
:param strings: list of strings
2728
"""
28-
strings.sort(key=_natural_key)
29+
strings.sort(key=_natural_key(case_sensitive))
2930
string.sort_natural = sort_natural
3031

3132

32-
def sorted_natural(lst):
33+
def sorted_natural(lst, case_sensitive=False):
3334
""" Simple function to return a sorted list of strings with numbers inside.
3435
3536
:param strings: list of strings
3637
:return: list of strings sorted based on numbers inside
3738
"""
38-
return sorted(lst, key=_natural_key)
39+
return sorted(lst, key=_natural_key(case_sensitive))
3940
string.sorted_natural = sorted_natural
4041

4142

0 commit comments

Comments
 (0)