I want to sort my array of objects in order from earliest to latest in time. I used the momentjs library to do this at first and it worked, but the method I was using is now deprecated.
At first I tried making the input time into a standard time(ex: 0500) and then sort it using that property. But I had trouble pushing this new standard time and all the other properties into the plans array.
I was thinking of using array.sort()
and sort it first by am/pm
and then by number. I'm not sure how to go about it since I have to deal with am
and pm
app.js
.controller('SchedulerController', schedulerCtrl);
function schedulerCtrl() {
this.plans = [
{
displayTime: '05:00pm',
duration: '30',
task: 'Team meeting',
},
{
displayTime: '06:00pm',
duration: '60',
task: 'Watch favorite TV show'
}
];
}
schedulerCtrl.prototype.addNewTask = function() {
var that = this;
that.plans.push(that.user);
that.user = '';
}
you can use underscorejs for an efficient sorting .
schedulerCtrl.prototype.addNewTask = function() {
var that = this;
that.plans.push(that.user);
_.sortBy(that.plans, function (item) { return item.displayTime });
that.user = '';
}
custom filter can be used ,
<p>{{item | orderBy:sortItemsAccording2Time}}</p>
var App = Angular.module('myApp',[])
App.filter('sortItemsAccording2Time',function(input,scope){
//logic here (service dependency can also be used for orderby service by just injecting the respective service which serves the job)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.