Skip to content

Commit 90cc99c

Browse files
authored
Merge pull request #9 from imcf/better-sort
Add function for better sorting and use it
2 parents 3a1d930 + b573d8b commit 90cc99c

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
<!-- markdownlint-disable MD024 (no-duplicate-header) -->
44

5+
## 1.4.0
6+
7+
### Added
8+
9+
* `imcflibs.strtools.sort_alphanumerically` to sort a list of strings taking
10+
into account numerical values correctly.
11+
12+
### Changed
13+
14+
* `imcflibs.pathtools.listdir_matching` is now using the new
15+
`sort_alphanumerically()` function from above.
16+
517
## 1.3.0
618

719
### Added

src/imcflibs/pathtools.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Helper functions to work with filenames, directories etc."""
22

3+
import os.path
34
import platform
45
from os import sep
5-
import os.path
66

7+
from . import strtools
78
from .log import LOG as log
89

910

@@ -169,8 +170,8 @@ def listdir_matching(path, suffix, fullpath=False, sort=False):
169170
paths to the matching files (the default is False, which will result in
170171
the file names only, without path).
171172
sort : bool, optional
172-
If set to True, the returned list will be sorted using Python's built-in
173-
`sorted()` call. By default False.
173+
If set to True, the returned list will be sorted using
174+
`imcflibs.strtools.sort_alphanumerically()`.
174175
175176
Returns
176177
-------
@@ -187,7 +188,7 @@ def listdir_matching(path, suffix, fullpath=False, sort=False):
187188
matching_files.append(candidate)
188189

189190
if sort:
190-
matching_files = sorted(matching_files)
191+
matching_files = strtools.sort_alphanumerically(matching_files)
191192

192193
return matching_files
193194

src/imcflibs/strtools.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""String related helper functions."""
22

3+
import re
4+
35

46
# this is taken from numpy's iotools:
57
def _is_string_like(obj):
@@ -109,3 +111,29 @@ def strip_prefix(string, prefix):
109111
if string.startswith(prefix):
110112
string = string[len(prefix) :]
111113
return string
114+
115+
116+
def sort_alphanumerically(data):
117+
"""Sort a list alphanumerically.
118+
119+
Parameters
120+
----------
121+
data : list
122+
List containing all the files to sort.
123+
124+
Returns
125+
-------
126+
list
127+
List with filenames sorted.
128+
129+
Examples
130+
--------
131+
>>> sorted([ "foo-1", "foo-2", "foo-10" ])
132+
["foo-1", "foo-10", "foo-2"]
133+
134+
>>> sort_alphanumerically([ "foo-1", "foo-2", "foo-10" ])
135+
["foo-1", "foo-2", "foo-10"]
136+
"""
137+
convert = lambda text: int(text) if text.isdigit() else text.lower()
138+
alphanum_key = lambda key: [convert(c) for c in re.split("([0-9]+)", key)]
139+
return sorted(data, key=alphanum_key)

0 commit comments

Comments
 (0)