function MyController($scope) {
var singleSort = [
{ "text": "Second", "a": 2 },
{ "text": "Fifth", "a": 5 },
{ "text": "First", "a": 1 },
{ "text": "Fourth", "a": 4 },
{ "text": "Third", "a": 3 }
];
var multipleSort = [
{ "text": "Second", "a": 1, "b": 2 },
{ "text": "Fifth", "a": 2, "b": 2 },
{ "text": "First", "a": 1, "b": 1 },
{ "text": "Fourth", "a": 2, "b": 1 },
{ "text": "Third", "a": 1, "b": 3 }
];
var singleSortIterator = function(item) {
return item.a;
};
var multipleSortIterator = function(item) {
return [item.a, item.b];
};
var singleSortReversedIterator = function(item) {
return -item.a;
};
var multipleSortReversedIterator = function(item) {
return -[item.a, item.b];
};
$scope.singleSort = _.sortBy(singleSort, singleSortIterator);
$scope.multipleSort = _.sortBy(multipleSort, multipleSortIterator);
$scope.singleSortReversed = _.sortBy(singleSort, singleSortReversedIterator);
$scope.multipleSortReversed = _.sortBy(multipleSort, multipleSortReversedIterator);
}
除了 multipleSortReversed 之外,排序算法都在工作。这里有什么明显的错误吗?
http://jsfiddle.net/andybooth/QEBUx/
我得到的结果是
Single sort
First
Second
Third
Fourth
Fifth
Multiple sort
First
Second
Third
Fourth
Fifth
Single sort (reversed)
Fifth
Fourth
Third
Second
First
Multiple sort (reversed)
Second
Fifth
First
Fourth
Third