Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort object of weekdays like Sunday, Monday, ..., Saturday [duplicate]

How to sort Javascript object of week days by weekdays i.e Here, is JSON format of object:

 {"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"]}

where key is day i.e."Wednesday". I want to sort it based on order of weekdays i.e Sunday, Monday, Tuesday etc

Expected Output is:

{ "Wednesday":["5:00pm to 11:00pm"], "Thursday":["5:00pm to 11:00pm"], "Friday":["5:00pm to 12:00am"], "Saturday":["11:00am to 12:00am"], "Sunday":["11:00am to 11:00pm"]}

this is what i was trying.

var keys = {}; 
Object.keys(scope.daySpecificHours)
.map(function (k) { return [k, scope.daySpecificHours[k]]; })
.sort(function (a, b) {
if (a[0] < b[0]) return -1;
if (a[0] > b[0]) return 1;
return 0;
})
.forEach(function (d) {
scope.daySpecificHours[d[0]] = d;
});

Thanks.

like image 851
Vishal Rajole Avatar asked Dec 03 '15 13:12

Vishal Rajole


People also ask

How to sort weekdays in JavaScript?

map(function (k) { return [k, scope. daySpecificHours[k]]; }) . sort(function (a, b) { if (a[0] < b[0]) return -1; if (a[0] > b[0]) return 1; return 0; }) .

Are JavaScript objects sorted?

Before you read the answers: The answer is No. The ordering of object properties is non-standard in ECMAScript. You should never make assumptions about the order of elements in a JavaScript object. An Object is an unordered collection of properties.

How do I sort an object in typescript?

Array. sort() function sorts an Array. The Sort() function will sort array using the optional compareFunction provided, if it is not provided Javascript will sort the array object by converting values to strings and comparing strings in UTF-16 code units order.


1 Answers

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>");
like image 59
Shanoor Avatar answered Sep 19 '22 06:09

Shanoor