Page 1 of 1

sorting array by day of the week.

Posted: Thu Apr 30, 2015 2:37 pm
by Joe Sharples

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&#46;length; i++){
if(data&#46;NightName == "Normal"){
othernightsArray&#46;push(data);
console&#46;log(data);
}
else {
mainnightsArray&#46;push(data);
}
}

localStorage&#46;setItem("OtherNights", JSON&#46;stringify(othernightsArray));
localStorage&#46;setItem("MainNights", JSON&#46;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&#46;Name < b&#46;Name)
return -1;
if (a&#46;Name b&#46;Name)
return 1;
return 0;
}

PeopleArray&#46;sort(compare);
/pre

but I'm not sure how modify this for Day (Monday, Tuesday, Wednesday, Thursday etc).

Any ideas?


sorting array by day of the week.

Posted: Thu Apr 30, 2015 3:04 pm
by Bruce Stuart

Joe, an alternative to consider might be sorting the data coming back from the service using the sort parameter. Just a thought ...might not be appropriate for what you're doing ...
Bruce


sorting array by day of the week.

Posted: Thu Apr 30, 2015 3:59 pm
by Joe Sharples

Thanks for your response.

I considered this also.
but how do I sort based on the day of the week.

Using sort parameter with the Day field returns the list in alphabetical order of the day field:

Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday

I'd like to have it sorted by the day in this order:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

I'm not sure how to sort it, either via a sort parameter, or through sorting the array.


sorting array by day of the week.

Posted: Thu Apr 30, 2015 4:20 pm
by Bruce Stuart

Joe,

Read here:

http://www.javascriptkit.com/javatuto...

  • you might consider adding a column to each element as you push data into your local storage array - for a value 1-7 that assigns each day of the week a value.

    And then use the tutorial here - on sorting an array of objects - based on the new value in the new column.

    or alternatively - you might consider adding a column to your data store - and then you can use native sorting from the database - as specified here:(which you are already aware of)

    http://docs.mongodb.org/manual/refere...

    Bruce


sorting array by day of the week.

Posted: Thu Apr 30, 2015 7:21 pm
by Joe Sharples

Thanks for the advice.

Managed to get my head around it.

pre
var othernightsArray = [];
var mainnightsArray = [];

function checkDayandOrder(){

if (data&#46;Day == "Monday"){
data&#46;order = "1";
}

if (data&#46;Day == "Tuesday"){
data&#46;order = "2";
}

if (data&#46;Day == "Wednesday"){
data&#46;order = "3";
}

if (data&#46;Day == "Thursday"){
data&#46;order = "4";
}

if (data&#46;Day == "Friday"){
data&#46;order = "5";
}

if (data[i]&#46;Day == "Saturday"){
data[i]&#46;order = "6";
}

if (data[i]&#46;Day == "Sunday"){
data[i]&#46;order = "7";
}

}

for(var i = 0; i < data&#46;length; i++){
if(data[i]&#46;NightName == "Normal"){
checkDayandOrder();
othernightsArray&#46;push(data[i]);
console&#46;log(data[i]);

}
else {
mainnightsArray&#46;push(data[i]);
checkDayandOrder();
}
}

function compareDay(a,b) {
if (a&#46;order < b&#46;order)
return -1;
if (a&#46;order b&#46;order)
return 1;
return 0;
}
othernightsArray&#46;sort(compareDay);
mainnightsArray&#46;sort(compareDay);

localStorage&#46;setItem("OtherNights", JSON&#46;stringify(othernightsArray));
localStorage&#46;setItem("MainNights", JSON&#46;stringify(mainnightsArray));
console&#46;log(othernightsArray);

/pre


sorting array by day of the week.

Posted: Sun May 03, 2015 3:50 am
by Bruce Stuart

congrats by the way Joe - very nice implementation....