diff --git a/chrome/content/zotero/integration/quickFormat.js b/chrome/content/zotero/integration/quickFormat.js index 08779ea86..bed9e5718 100644 --- a/chrome/content/zotero/integration/quickFormat.js +++ b/chrome/content/zotero/integration/quickFormat.js @@ -247,6 +247,7 @@ var Zotero_QuickFormat = new function () { // No need to refresh anything if box hasn't changed if(searchResultIDs.length === curIDs.length) { + Zotero.debug("unchanged"); var mismatch = false; for(var i=0; i 50-citedItems.length) { - searchResultIDs = searchResultIDs.slice(0, 50-citedItems.length); - } - var items = Zotero.Items.get(searchResultIDs); - items.sort(_itemSort); + + items.sort(function _itemSort(a, b) { + var libA = a.libraryID ? a.libraryID : 0, libB = b.libraryID ? b.libraryID : 0; + if(libA !== libB) { + Zotero.debug(libA); + Zotero.debug(libB); + // Sort by number of cites for library + if(nCitedItemsFromLibrary[libA] && !nCitedItemsFromLibrary[libB]) { + return -1; + } + if(!nCitedItemsFromLibrary[libA] && nCitedItemsFromLibrary[libB]) { + return 1; + } + if(nCitedItemsFromLibrary[libA] !== nCitedItemsFromLibrary[libB]) { + return nCitedItemsFromLibrary[libB] - nCitedItemsFromLibrary[libA]; + } + + // Sort by ID even if number of cites is equal + return libA - libB; + } + + // Sort by last name of first author + var creatorsA = a.getCreators(), creatorsB = b.getCreators(), + caExists = creatorsA.length ? 1 : 0, cbExists = creatorsB.length ? 1 : 0; + if(caExists !== cbExists) { + return cbExists-caExists; + } else if(caExists) { + return creatorsA[0].ref.lastName.localeCompare(creatorsB[0].ref.lastName); + } + + // Sort by date + var yearA = a.getField("date", true, true).substr(0, 4), + yearB = b.getField("date", true, true).substr(0, 4); + return yearA - yearB; + }); var previousLibrary = -1; - for(var i=0, n=items.length; i