function sort_days(days) {
To get today's day of week, use new Date().getDay()
. This assumes Sunday = 0, Monday = 1, ..., Saturday = 6
.
var day_of_week = new Date().getDay();
To generate the list of the days of week, then slice the list of names:
var list = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var sorted_list = list.slice(day_of_week).concat(list.slice(0,day_of_week));
(today is Friday, so sorted_list
is ['Fri','Sat','Sun','Mon','Tue','Wed','Thu']
)
Finally, to sort, use indexOf
:
return days.sort(function(a,b) { return sorted_list.indexOf(a) > sorted_list.indexOf(b); });
}
Putting it all together:
function sort_days(days) {
var day_of_week = new Date().getDay();
var list = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var sorted_list = list.slice(day_of_week).concat(list.slice(0,day_of_week));
return days.sort(function(a,b) { return sorted_list.indexOf(a) > sorted_list.indexOf(b); });
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…