我似乎无法以相反的顺序显示此处排序的项目。我想在顶部而不是底部显示添加到列表中的最后一项:
周以降序(正确)显示,但项目则没有。我也使用比较将已完成的任务移到底部。
app.directive('repeatByWeek', function($parse, $window) {
return {
restrict: 'E',
transclude: true,
template: '<div ng-transclude></div>',
link: function(scope, el, attrs) {
// get the global moment lib
var moment = $window.moment;
scope.weeks = [];
updateList();
// whenever the source collection changes, update our nested list
scope.list.$watch(updateList);
scope.$watch(function(){
return scope.list;
}, updateList);
function compare(a, b) {
return a.completeTime - b.completeTime;
}
function updateList() {
scope.list.$loaded().then(function(){
scope.list.sort(compare);
scope.weeks = sortItems(parseItems(scope.list));
});
}
function sortItems(sets) {
var items = [];
// get a list of weeks and sort them
var weeks = sortDescending(Object.keys(sets));
for(var i=0, wlen=weeks.length; i < wlen; i++) {
var w = weeks[i];
// get a list of days and sort them
var days = Object.keys(sets[w]);
var weekEntry = {
time: w,
days: []
};
items.push(weekEntry);
// now iterate the days and add entries
for(var j=0, dlen=days.length; j < dlen; j++) {
var d = days[j];
weekEntry.days.push({
time: d,
// here is the list of tasks from parseItems
items: sets[w][d]
});
}
}
// console.log('sortItems', items);
return items;
}
// take the array and nest it in an object by week and then day
function parseItems(list) {
var sets = {};
angular.forEach(list, function(item) {
var week = moment(item.$priority).startOf('week').valueOf();
var day = moment(item.$priority).startOf('day').valueOf();
if( !sets.hasOwnProperty(week) ) {
sets[week] = {};
}
if( !sets[week].hasOwnProperty(day) ) {
sets[week][day] = [];
}
sets[week][day].push(item);
});
// console.log('parseItems', sets);
return sets;
}
function sortDescending(list) {
return list.sort().reverse();
}
}
};
});
任何建议都会受到高度赞赏?我已经尝试了各种方法将“reverse()”添加到列表中,但这似乎不起作用。
试图远:
为sortDescending(项目) scope.items.reverse() scope.items.sort()。反向()
我能反过来推它们吗?我在比较时搞砸了这种情况吗?