fixed bug #1280295 'Enable natural sorting for song book searches' --fixes 1280294

This commit is contained in:
Chris Hill 2015-02-13 18:29:42 +00:00
parent 88cb02bb3d
commit c8f200b20f
1 changed files with 19 additions and 1 deletions

View File

@ -255,7 +255,7 @@ class SongMediaItem(MediaManagerItem):
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 @@ class SongMediaItem(MediaManagerItem):
# 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