add Ox.sortBy

This commit is contained in:
rlx 2012-01-11 16:17:06 +05:30
parent 923f71e6de
commit 11d9b63dba
2 changed files with 98 additions and 44 deletions

View file

@ -156,9 +156,9 @@ Ox.AnnotationPanel = function(options, self) {
self.setOption = function(key, value) {
if (['in', 'out'].indexOf(key) > -1 && self.editing) {
var index = Ox.getIndexById(self.options.items, self.options.selected);
self.options.items[index][key] = value;
self.options.items[index].duration = self.options.out - self.options['in'];
var item = Ox.getObjectById(self.options.items, self.options.selected);
items[key] = value;
items.duration = self.options.out - self.options['in'];
}
if (key == 'in') {
//fixme: array editable should support item updates while editing

View file

@ -83,20 +83,12 @@ Ox.range = function() {
return arr;
};
/*@
Ox.sort <f> Sorts an array, handling leading digits and ignoring capitalization
arr <a> Array
fn <f|u> Optional map function that returns the value for the array element
> Ox.sort(['10', '9', 'B', 'a'])
['9', '10', 'a', 'B']
> Ox.sort([{id: 0, name: '80 Days'}, {id: 1, name: '8 Women'}], function(v) {return v.name});
[{id: 1, name: '8 Women'}, {id: 0, name: '80 Days'}]
@*/
Ox.sort = function(arr, fn) {
var len, matches = {}, sort = {},
values = fn ? arr.map(fn) : arr;
(function() {
function getSortValues(arr) {
var len, matches = {}, sort = {};
// find leading numbers
values.forEach(function(val, i) {
arr.forEach(function(val) {
var match = /^\d+/.exec(val);
matches[val] = match ? match[0] : '';
});
@ -104,14 +96,31 @@ Ox.sort = function(arr, fn) {
len = Ox.max(Ox.map(matches, function(val) {
return val.length;
}));
// pad leading numbers, and make lower case
values.forEach(function(val) {
// pad leading numbers and make lowercase
arr.forEach(function(val) {
sort[val] = (
matches[val] ? Ox.pad(
matches[val], len
) + val.toString().substr(matches[val].length) : val
matches[val]
? Ox.pad(matches[val], len)
+ val.toString().substr(matches[val].length)
: val
).toLowerCase();
});
return sort;
}
/*@
Ox.sort <f> Sorts an array, handling leading digits and ignoring capitalization
(arr) -> <a> Sorted array
(arr, fn) -> Sorted array
arr <a> Array
fn <f|u> Optional map function that returns the value for the array element
> Ox.sort(['10', '9', 'B', 'a'])
['9', '10', 'a', 'B']
> Ox.sort([{id: 0, name: '80 Days'}, {id: 1, name: '8 Women'}], function(v) {return v.name})
[{id: 1, name: '8 Women'}, {id: 0, name: '80 Days'}]
@*/
Ox.sort = function(arr, fn) {
var sort = getSortValues(fn ? arr.map(fn) : arr);
return arr.sort(function(a, b) {
a = fn ? fn(a) : a;
b = fn ? fn(b) : b;
@ -125,6 +134,51 @@ Ox.sort = function(arr, fn) {
});
};
/*@
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([1, 2, 3, 2, 1])