fix alphabetical order of annotations with tags, fixes #681

This commit is contained in:
rlx 2012-03-20 09:34:50 +00:00
commit bc3fedb125
4 changed files with 30 additions and 17 deletions

View file

@ -85,24 +85,27 @@ Ox.range = function() {
(function() {
function getSortValues(arr) {
var len, matches = {}, sort = {};
function getSortValues(arr, fn) {
var arr_ = fn ? arr.map(fn) : arr,
len, matches = {}, sort = {};
// find leading numbers
arr.forEach(function(val) {
var match = /^\d+/.exec(val);
arr.forEach(function(val, i) {
var match = /^\d+/.exec(arr_[i]);
matches[val] = match ? match[0] : '';
});
// get length of longest leading number
len = Ox.max(Ox.map(matches, function(val) {
return val.length;
}));
// pad leading numbers and make lowercase
arr.forEach(function(val) {
// pad leading numbers, make lowercase,
// and remove leading non-word characters
arr.forEach(function(val, i) {
sort[val] = (
matches[val]
? Ox.pad(matches[val], len)
+ val.toString().substr(matches[val].length)
: val
? arr_[i].toString().replace(
matches[val], Ox.pad(matches[val], len)
)
: arr_[i]
).toLowerCase().replace(/^\W+/, '');
});
return sort;
@ -138,13 +141,14 @@ Ox.range = function() {
Ox.sortBy <f> Sorts an array of objects by given properties
(arr, by) -> <a> Sorted array
arr <[o]> Array of objects
by <[s]> Array of object properties (asc: 'foo' or '+foo', desc: '-foo')
by <[s]> Array of object keys (asc: 'foo' or '+foo', desc: '-foo')
fn <o> Optional functions, per key, that return the sort value
> Ox.sortBy([{x: 1, y: 1}, {x: 1, y: 2}, {x: 2, y: 2}], ['+x', '-y'])
[{x: 1, y: 2}, {x: 1, y: 1}, {x: 2, y: 2}]
> Ox.sortBy([{id: 0, name: '80 Days'}, {id: 1, name: '8 Women'}], ['name'])
[{id: 1, name: '8 Women'}, {id: 0, name: '80 Days'}]
@*/
Ox.sortBy = function(arr, by) {
Ox.sortBy = function(arr, by, fn) {
var length = by.length, values = {};
by = by.map(function(v) {
return {
@ -152,12 +156,13 @@ Ox.range = function() {
operator: v[0] == '-' ? '-' : '+'
};
});
fn = fn || {};
by.map(function(v) {
return v.key;
}).forEach(function(key) {
values[key] = getSortValues(arr.map(function(v) {
return v[key];
}));
}), fn[key]);
});
return arr.sort(function(a, b) {
var aValue, bValue, index = 0, key, ret = 0;