Sort object of weekdays like Sunday, Monday, ..., Saturday [duplicate]
Like I said in my comment, you can't sort an object but if you can change your data's format to have an array, you can easily sort it by using [].sort
let data = [ { day: "Friday", hours: ["5:00pm to 12:00am"] }, { day: "Wednesday", hours: ["5:00pm to 11:00pm"] }, { day: "Sunday", hours: ["11:00am to 11:00pm"] }, { day: "Thursday", hours: ["5:00pm to 11:00pm"] }, { day: "Saturday", hours: ["11:00am to 12:00am"] }];const sorter = { // "sunday": 0, // << if sunday is first day of week "monday": 1, "tuesday": 2, "wednesday": 3, "thursday": 4, "friday": 5, "saturday": 6, "sunday": 7}data.sort(function sortByDay(a, b) { let day1 = a.day.toLowerCase(); let day2 = b.day.toLowerCase(); return sorter[day1] - sorter[day2];});console.log(data);document.write("<pre>" + JSON.stringify(data, null, 3) + "</pre>");
edit
To "order" your object's keys by day of the week, again order is not guaranteed, use at your own risk
let data = { "Friday": ["5:00pm to 12:00am"], "Wednesday": ["5:00pm to 11:00pm"], "Sunday": ["11:00am to 11:00pm"], "Thursday": ["5:00pm to 11:00pm"], "Saturday": ["11:00am to 12:00am"]};const sorter = { "monday": 1, "tuesday": 2, "wednesday": 3, "thursday": 4, "friday": 5, "saturday": 6, "sunday": 7};let tmp = [];Object.keys(data).forEach(function(key) { let value = data[key]; let index = sorter[key.toLowerCase()]; tmp[index] = { key: key, value: value };});let orderedData = {};tmp.forEach(function(obj) { orderedData[obj.key] = obj.value;});console.log(orderedData);document.write("<pre>" + JSON.stringify(orderedData, null, 3) + "</pre>");
Just as a proof of concept using ES6, this should work Requires MomentJS:
const unordered = {"Friday":["5:00pm to 12:00am"] ,"Wednesday":["5:00pm to 11:00pm"],"Sunday":["11:00am to 11:00pm"], "Thursday":["5:00pm to 11:00pm"],"Saturday":["11:00am to 12:00am"]};const ordered = {};Object.keys(unordered).sort(function (a, b) { return moment(a, 'ddd dddd').weekday() > moment(b, 'ddd dddd').weekday();}).forEach(function(key) { ordered[key] = unordered[key];});console.log(ordered);
This returns:
{ Sunday: [ '11:00am to 11:00pm' ], Wednesday: [ '5:00pm to 11:00pm' ], Thursday: [ '5:00pm to 11:00pm' ], Friday: [ '5:00pm to 12:00am' ], Saturday: [ '11:00am to 12:00am' ] }
Note: This is mainly a proof of concept to show it is possible, there are issues such as MomentJS treating Sunday as day 0 (based on user locale etc). You could easily write your own mapping of weekdays to int (Monday -> 0, Tuesday -> 1) and remove the MomentJS dependency.
EDIT:Expanding on above:
var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
Then change the sort function to
return days.indexOf(a) > days.indexOf(b)
const unordered = {"Friday":["5:00pm to 12:00am"] ,"Wednesday":["5:00pm to 11:00pm"],"Sunday":["11:00am to 11:00pm"], "Thursday":["5:00pm to 11:00pm"],"Saturday":["11:00am to 12:00am"]};var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']const ordered = {};Object.keys(unordered).sort(function (a, b) { return days.indexOf(a) > days.indexOf(b);}).forEach(function(key) { ordered[key] = unordered[key];});console.log(ordered);
Example: http://www.es6fiddle.net/ihqg46kp/