Sort collections and items using locale-specific collation sort

This commit is contained in:
Dan Stillman 2007-04-16 05:00:32 +00:00
parent 0dbf98ccf4
commit 2f423da309
2 changed files with 27 additions and 11 deletions

View File

@ -4548,22 +4548,29 @@ Zotero.getCollections = function(parent, recursive){
parent = null; parent = null;
} }
var sql = 'SELECT collectionID FROM collections C WHERE parentCollectionID'; var sql = "SELECT collectionID AS id, collectionName AS name FROM collections C "
sql += parent ? '=' + parent : ' IS NULL'; + "WHERE parentCollectionID " + (parent ? '=' + parent : ' IS NULL');
var children = Zotero.DB.query(sql);
sql += ' ORDER BY collectionName COLLATE NOCASE';
var children = Zotero.DB.columnQuery(sql);
if (!children){ if (!children){
Zotero.debug('No child collections of collection ' + parent, 5); Zotero.debug('No child collections of collection ' + parent, 5);
return toReturn; return toReturn;
} }
// Do proper collation sort
var localeService = Components.classes["@mozilla.org/intl/nslocaleservice;1"]
.getService(Components.interfaces.nsILocaleService);
var collationFactory = Components.classes["@mozilla.org/intl/collation-factory;1"]
.getService(Components.interfaces.nsICollationFactory);
var collation = collationFactory.CreateCollation(localeService.getApplicationLocale());
children.sort(function (a, b) {
return collation.compareString(0, a.name, b.name);
});
for (var i=0, len=children.length; i<len; i++){ for (var i=0, len=children.length; i<len; i++){
var obj = Zotero.Collections.get(children[i]); var obj = Zotero.Collections.get(children[i].id);
if (!obj){ if (!obj){
throw ('Collection ' + children[i] + ' not found'); throw ('Collection ' + children[i].id + ' not found');
} }
toReturn.push(obj); toReturn.push(obj);

View File

@ -718,6 +718,12 @@ Zotero.ItemTreeView.prototype.sort = function(itemID)
var columnField = this.getSortField(); var columnField = this.getSortField();
var order = this.getSortDirection() == 'ascending'; var order = this.getSortDirection() == 'ascending';
var localeService = Components.classes["@mozilla.org/intl/nslocaleservice;1"]
.getService(Components.interfaces.nsILocaleService);
var collationFactory = Components.classes["@mozilla.org/intl/collation-factory;1"]
.getService(Components.interfaces.nsICollationFactory);
var collation = collationFactory.CreateCollation(localeService.getApplicationLocale());
// Year is really the date field truncated // Year is really the date field truncated
if (columnField == 'year') { if (columnField == 'year') {
columnField = 'date'; columnField = 'date';
@ -798,7 +804,8 @@ Zotero.ItemTreeView.prototype.sort = function(itemID)
} }
} }
cmp = (fieldA > fieldB) ? -1 : (fieldA < fieldB) ? 1 : 0; //cmp = (fieldA > fieldB) ? -1 : (fieldA < fieldB) ? 1 : 0;
cmp = collation.compareString(0, fieldA, fieldB);
if (cmp) { if (cmp) {
return cmp; return cmp;
} }
@ -815,7 +822,8 @@ Zotero.ItemTreeView.prototype.sort = function(itemID)
return cmp; return cmp;
} }
cmp = (fieldA > fieldB) ? -1 : (fieldA < fieldB) ? 1 : 0; //cmp = (fieldA > fieldB) ? -1 : (fieldA < fieldB) ? 1 : 0;
cmp = collation.compareString(0, fieldA, fieldB);
if (cmp) { if (cmp) {
return cmp; return cmp;
} }
@ -840,7 +848,8 @@ Zotero.ItemTreeView.prototype.sort = function(itemID)
fieldA = a.getField('dateModified'); fieldA = a.getField('dateModified');
fieldB = b.getField('dateModified'); fieldB = b.getField('dateModified');
return (fieldA > fieldB) ? -1 : (fieldA < fieldB) ? 1 : 0; //return (fieldA > fieldB) ? -1 : (fieldA < fieldB) ? 1 : 0;
return collation.compareString(0, fieldA, fieldB);
} }
function doSort(a,b) function doSort(a,b)