add Ox.sortBy
This commit is contained in:
parent
923f71e6de
commit
11d9b63dba
2 changed files with 98 additions and 44 deletions
|
@ -156,9 +156,9 @@ Ox.AnnotationPanel = function(options, self) {
|
||||||
|
|
||||||
self.setOption = function(key, value) {
|
self.setOption = function(key, value) {
|
||||||
if (['in', 'out'].indexOf(key) > -1 && self.editing) {
|
if (['in', 'out'].indexOf(key) > -1 && self.editing) {
|
||||||
var index = Ox.getIndexById(self.options.items, self.options.selected);
|
var item = Ox.getObjectById(self.options.items, self.options.selected);
|
||||||
self.options.items[index][key] = value;
|
items[key] = value;
|
||||||
self.options.items[index].duration = self.options.out - self.options['in'];
|
items.duration = self.options.out - self.options['in'];
|
||||||
}
|
}
|
||||||
if (key == 'in') {
|
if (key == 'in') {
|
||||||
//fixme: array editable should support item updates while editing
|
//fixme: array editable should support item updates while editing
|
||||||
|
|
|
@ -83,47 +83,101 @@ Ox.range = function() {
|
||||||
return arr;
|
return arr;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
(function() {
|
||||||
Ox.sort <f> Sorts an array, handling leading digits and ignoring capitalization
|
|
||||||
arr <a> Array
|
function getSortValues(arr) {
|
||||||
fn <f|u> Optional map function that returns the value for the array element
|
var len, matches = {}, sort = {};
|
||||||
> Ox.sort(['10', '9', 'B', 'a'])
|
// find leading numbers
|
||||||
['9', '10', 'a', 'B']
|
arr.forEach(function(val) {
|
||||||
> Ox.sort([{id: 0, name: '80 Days'}, {id: 1, name: '8 Women'}], function(v) {return v.name});
|
var match = /^\d+/.exec(val);
|
||||||
[{id: 1, name: '8 Women'}, {id: 0, name: '80 Days'}]
|
matches[val] = match ? match[0] : '';
|
||||||
@*/
|
});
|
||||||
Ox.sort = function(arr, fn) {
|
// get length of longest leading number
|
||||||
var len, matches = {}, sort = {},
|
len = Ox.max(Ox.map(matches, function(val) {
|
||||||
values = fn ? arr.map(fn) : arr;
|
return val.length;
|
||||||
// find leading numbers
|
}));
|
||||||
values.forEach(function(val, i) {
|
// pad leading numbers and make lowercase
|
||||||
var match = /^\d+/.exec(val);
|
arr.forEach(function(val) {
|
||||||
matches[val] = match ? match[0] : '';
|
sort[val] = (
|
||||||
});
|
matches[val]
|
||||||
// get length of longest leading number
|
? Ox.pad(matches[val], len)
|
||||||
len = Ox.max(Ox.map(matches, function(val) {
|
+ val.toString().substr(matches[val].length)
|
||||||
return val.length;
|
: val
|
||||||
}));
|
).toLowerCase();
|
||||||
// pad leading numbers, and make lower case
|
});
|
||||||
values.forEach(function(val) {
|
return sort;
|
||||||
sort[val] = (
|
}
|
||||||
matches[val] ? Ox.pad(
|
|
||||||
matches[val], len
|
/*@
|
||||||
) + val.toString().substr(matches[val].length) : val
|
Ox.sort <f> Sorts an array, handling leading digits and ignoring capitalization
|
||||||
).toLowerCase();
|
(arr) -> <a> Sorted array
|
||||||
});
|
(arr, fn) -> Sorted array
|
||||||
return arr.sort(function(a, b) {
|
arr <a> Array
|
||||||
a = fn ? fn(a) : a;
|
fn <f|u> Optional map function that returns the value for the array element
|
||||||
b = fn ? fn(b) : b;
|
> Ox.sort(['10', '9', 'B', 'a'])
|
||||||
var ret = 0;
|
['9', '10', 'a', 'B']
|
||||||
if (sort[a] < sort[b]) {
|
> Ox.sort([{id: 0, name: '80 Days'}, {id: 1, name: '8 Women'}], function(v) {return v.name})
|
||||||
ret = -1;
|
[{id: 1, name: '8 Women'}, {id: 0, name: '80 Days'}]
|
||||||
} else if (sort[a] > sort[b]) {
|
@*/
|
||||||
ret = 1;
|
Ox.sort = function(arr, fn) {
|
||||||
}
|
var sort = getSortValues(fn ? arr.map(fn) : arr);
|
||||||
return ret;
|
return arr.sort(function(a, b) {
|
||||||
});
|
a = fn ? fn(a) : a;
|
||||||
};
|
b = fn ? fn(b) : b;
|
||||||
|
var ret = 0;
|
||||||
|
if (sort[a] < sort[b]) {
|
||||||
|
ret = -1;
|
||||||
|
} else if (sort[a] > sort[b]) {
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/*@
|
||||||
|
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')
|
||||||
|
> 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) {
|
||||||
|
var length = by.length, values = {};
|
||||||
|
by = by.map(function(v) {
|
||||||
|
return {
|
||||||
|
key: v.replace(/^[\+\-]/g, ''),
|
||||||
|
operator: v[0] == '-' ? '-' : '+'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
by.map(function(v) {
|
||||||
|
return v.key
|
||||||
|
}).forEach(function(key) {
|
||||||
|
values[key] = getSortValues(arr.map(function(v) {
|
||||||
|
return v[key];
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
return arr.sort(function(a, b) {
|
||||||
|
var aValue, bValue, index = 0, key, ret = 0;
|
||||||
|
while (ret == 0 && index < length) {
|
||||||
|
key = by[index].key;
|
||||||
|
aValue = values[key][a[key]];
|
||||||
|
bValue = values[key][b[key]];
|
||||||
|
if (aValue < bValue) {
|
||||||
|
ret = by[index].operator == '+' ? -1 : 1;
|
||||||
|
} else if (aValue > bValue) {
|
||||||
|
ret = by[index].operator == '+' ? 1 : -1;
|
||||||
|
} else {
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
}());
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
Ox.unique <f> Returns an array without duplicate values
|
Ox.unique <f> Returns an array without duplicate values
|
||||||
|
|
Loading…
Reference in a new issue