The above page tells us how to sort the items in a JavaScript array that are not key/value pairs. So you see the months sort alphabetically, but it results in out of order for the yearly sequence.
For key/value arrays in JS: How would one sort according to the keys if the order is not sorted already?
Note the order numbers have been reformatted with the zero’s removed. If you want to keep the zeros they would have to be strings e.g. month_order: "02".
No. Array.prototype.sort mutates the original array.
These are the arguments that the callback that you are passing to sort is receiving. These will correspond to the array elements as they are put into the desired order (try logging them to the console).
No. months is an array. It doesn’t have a month property.
So if we had three instead of two per list, I would use “function(a,b)” to get the same results, which is assuming the script “knows” a and b are the first two in the list?
{
month_order: 01,
month_name:"January",
month_holiday:"New Years Day"
},
If I want to sort the same array in different ways, should I duplicate the array before doing a sort so the original remains intact, then remove the duplicate?