openlp-core team mailing list archive
-
openlp-core team
-
Mailing list archive
-
Message #26101
[Merge] lp:~minkus/openlp/naturalsortsongs into lp:openlp
Chris Hill has proposed merging lp:~minkus/openlp/naturalsortsongs into lp:openlp.
Requested reviews:
OpenLP Core (openlp-core)
For more details, see:
https://code.launchpad.net/~minkus/openlp/naturalsortsongs/+merge/249699
Fixes bug 1280295 - 'Enable natural sorting for song book searches' - based on code found in setup.py
--
Your team OpenLP Core is requested to review the proposed merge of lp:~minkus/openlp/naturalsortsongs into lp:openlp.
=== modified file 'openlp/plugins/songs/lib/mediaitem.py'
--- openlp/plugins/songs/lib/mediaitem.py 2015-01-18 13:39:21 +0000
+++ openlp/plugins/songs/lib/mediaitem.py 2015-02-13 18:30:53 +0000
@@ -255,7 +255,7 @@
log.debug('display results Book')
self.list_view.clear()
for book in search_results:
- songs = sorted(book.songs, key=lambda song: int(re.match(r'[0-9]+', '0' + song.song_number).group()))
+ songs = sorted(book.songs, key=lambda song: self._natural_sort_key(song.song_number))
for song in songs:
# Do not display temporary songs
if song.temporary:
@@ -583,6 +583,24 @@
# List must be empty at the end
return not author_list
+ def _try_int(self, s):
+ """
+ Convert string s to an integer if possible. Fail silently and return
+ the string as-is if it isn't an integer.
+ :param s: The string to try to convert.
+ """
+ try:
+ return int(s)
+ except (TypeError, ValueError):
+ return s
+
+ def _natural_sort_key(self, s):
+ """
+ Return a tuple by which s is sorted.
+ :param s: A string value from the list we want to sort.
+ """
+ return list(map(self._try_int, re.findall(r'(\d+|\D+)', s)))
+
def search(self, string, show_error):
"""
Search for some songs
Follow ups