From 3c4d8500e514c9d36732e9f692f60d7de9bc60d6 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sun, 25 Mar 2012 17:35:07 -0700 Subject: [PATCH] sort by sort names --- beets/library.py | 15 +++++++++++++-- docs/changelog.rst | 3 ++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/beets/library.py b/beets/library.py index b87ac0797..b33ca8919 100644 --- a/beets/library.py +++ b/beets/library.py @@ -118,6 +118,15 @@ log = logging.getLogger('beets') if not log.handlers: log.addHandler(logging.StreamHandler()) +# A little SQL utility. +def _orelse(exp1, exp2): + """Generates an SQLite expression that evaluates to exp1 if exp1 is + non-null and non-empty or exp2 otherwise. + """ + return ('(CASE {0} WHEN NULL THEN {1} ' + 'WHEN "" THEN {1} ' + 'ELSE {0} END)').format(exp1, exp2) + # Exceptions. @@ -1042,7 +1051,8 @@ class Library(BaseLibrary): where, subvals = query.clause() sql = "SELECT * FROM albums " + \ "WHERE " + where + \ - " ORDER BY albumartist, album" + " ORDER BY %s, album" % \ + _orelse("albumartist_sort", "albumartist") c = self.conn.execute(sql, subvals) return [Album(self, dict(res)) for res in c.fetchall()] @@ -1059,7 +1069,8 @@ class Library(BaseLibrary): sql = "SELECT * FROM items " + \ "WHERE " + where + \ - " ORDER BY artist, album, disc, track" + " ORDER BY %s, album, disc, track" % \ + _orelse("artist_sort", "artist") log.debug('Getting items with SQL: %s' % sql) c = self.conn.execute(sql, subvals) return ResultIterator(c) diff --git a/docs/changelog.rst b/docs/changelog.rst index a2543c1be..c4284b211 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -12,7 +12,8 @@ Changelog for details. * Artist **sort names** are now fetched from MusicBrainz. There are two new data fields, ``artist_sort`` and ``albumartist_sort``, that contain sortable artist - names like "Beatles, The". Thanks to Paul Provost. + names like "Beatles, The". These fields are also used to sort albums and items + when using the ``list`` command. Thanks to Paul Provost. * New :doc:`/plugins/rdm`: Randomly select albums and tracks from your library. Thanks to Philippe Mongeau. * The :doc:`/plugins/mbcollection` by Jeffrey Aylesworth was added to the core