From 41e2f3008e50fd2803a3c22cb43ed8c9f00714a5 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 19 Mar 2013 15:47:04 -0400 Subject: [PATCH] Sort empty titles last when title isn't primary sort When sorting by Title, empty titles get sorted to the top of the items list for visibility, but when sorting by another column and using the title as a secondary/tertiary sort, empty titles should get sorted last so that new empty items go to the end of the list rather than the middle. This is a little weird, and the alternative would be to just always sort empty titles last even when sorting by Title, but this preserves the current behavior for Title sorting. (Before f0f6772b01a titles weren't used for secondary sorting at all, so there's no precedent for title-sorting behavior when sorting by another column.) Addresses #275 --- chrome/content/zotero/xpcom/itemTreeView.js | 28 +++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/chrome/content/zotero/xpcom/itemTreeView.js b/chrome/content/zotero/xpcom/itemTreeView.js index 863418840f..645ff345da 100644 --- a/chrome/content/zotero/xpcom/itemTreeView.js +++ b/chrome/content/zotero/xpcom/itemTreeView.js @@ -1287,10 +1287,20 @@ Zotero.ItemTreeView.prototype.sort = function(itemID) var unformatted = false; } - // Hash table of fields for which rows with empty values should be displayed last - var emptyFirst = { - title: true - }; + // Set whether rows with empty values should be displayed last, + // which may be different for primary and secondary sorting. + var emptyFirst = {}; + switch (columnField) { + case 'title': + emptyFirst.title = true; + break; + + // When sorting by title we want empty titles at the top, but if not + // sorting by title, empty titles should sort to the bottom so that new + // empty items don't get sorted to the middle of the items list. + default: + emptyFirst.title = false; + } // Cache primary values while sorting, since base-field-mapped getField() // calls are relatively expensive @@ -1459,7 +1469,15 @@ Zotero.ItemTreeView.prototype.sort = function(itemID) } if (columnField !== 'title') { - cmp = collation.compareString(1, b.getField('title', true), a.getField('title', true)); + fieldA = a.getField('title', true); + fieldB = b.getField('title', true); + + if (!emptyFirst.title) { + if (fieldA === '' && fieldB !== '') return -1; + if (fieldA !== '' && fieldB === '') return 1; + } + + cmp = collation.compareString(1, fieldB, fieldA); if (cmp !== 0) { return cmp; }