sorting array by day of the week.
Hi guys,
How do i sort an array of objects by the day of the week ( Monday, Tuesday, Wednesday, Thursday etc)?
I have a service response that pushes the data into and array and then saves the array in local storage.. this is then mapped to 2 separate lists.
pre
othernightsArray = [];
mainnightsArray = [];
for(var i = 0; i < data.length; i++){
if(data.NightName == "Normal"){
othernightsArray.push(data);
console.log(data);
}
else {
mainnightsArray.push(data);
}
}
localStorage.setItem("OtherNights", JSON.stringify(othernightsArray));
localStorage.setItem("MainNights", JSON.stringify(mainnightsArray));
/pre
This works fine but before setting the local storage I want to sort the array by the day of the week.
Each object has a "Day" field.
I've used a compare function in my app to sort an array by Name alphabetically
pre
function compare(a,b) {
if (a.Name < b.Name)
return -1;
if (a.Name b.Name)
return 1;
return 0;
}
PeopleArray.sort(compare);
/pre
but I'm not sure how modify this for Day (Monday, Tuesday, Wednesday, Thursday etc).
Any ideas?