angularjs - JavaScript sorting on date (but not the "usual" way) -
i'm trying sort js array, in different fashion. here's scenario:
say have following dates in array (these each item
's expirydate
)
1. 2015/09/23 2. 2015/10/10 3. 2015/07/05 4. 2015/07/24
and current date 2015/07/04
, select 2015/09
(which vm.sortmodel
in example) sorting date display order must follows -
3. 1. 4. 2.
here's have far, it's not working...
for (var = 0; < vm.tabledata.length; i++) { var entity = vm.tabledata[i]; entity.items.sort(function(a, b) { if (a.expiresthisweek() < b.expiresthisweek()) { return 1; } //now check stuff in months after selected year/month combo if (vm.sortmodel) { var tmp = vm.sortmodel.split('/'); var currentdateselection = new date(tmp[0], tmp[1]); if (a.getexpirydateobject() >= currentdateselection) { return 1; } else { return -1; } } if (a.getexpirydateobject() < b.getexpirydateobject()) { return -1; } else if (a.getexpirydateobject() > b.getexpirydateobject()) { return 1; } return 0; }); }
basically, if item expires in week, must @ top of table, should list items in "selected month", months after month, , then months before that.
very strange, know, business gets business wants.
thanks help!
edit
here's expiresthisweek
method
model.expiresthisweek = function() { var todaydate = new date(); //we want check if item expires within 7 days today var enddate = new date(todaydate.getfullyear(), todaydate.getmonth(), todaydate.getdate() + 7); var date = model.getexpirydateobject(); return todaydate < date && date < enddate; }
Comments
Post a Comment