Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
How to sort an object based on key (weekday)

I need this array sorted by key (weekday). Currently it is displayed as sunday - friday

"weekOverview": {
"Sunday": 0.1111111111112,
"Monday": 0.1111111111111,
"Tuesday": 0.1100204936741015,
"Wednesday": 0.8655145400508841,
"Thursday": 0.2206230378805767,
"Friday": 0.7633485206232166,
"Saturday": 1.6047637274510986

},

Yet I need it displayed as monday - sunday

"weekOverview": {
"Monday": 0.1111111111111,
"Tuesday": 0.1100204936741015,
"Wednesday": 0.8655145400508841,
"Thursday": 0.2206230378805767,
"Friday": 0.7633485206232166,
"Saturday": 1.6047637274510986
"Sunday": 0.1111111111112,

},

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is a sortByDays method which does exactly what you want it to do.

We utilize Object.keys, as well as a custom sorter object.

const weekOverview = {
  Sunday: 0.1111111111112,
  Monday: 0.1111111111111,
  Tuesday: 0.1100204936741015,
  Wednesday: 0.8655145400508841,
  Thursday: 0.2206230378805767,
  Friday: 0.7633485206232166,
  Saturday: 1.6047637274510986,
};

const sortByDays = (obj) => {
  const sorter = {
    Monday: 1,
    Tuesday: 2,
    Wednesday: 3,
    Thursday: 4,
    Friday: 5,
    Saturday: 6,
    Sunday: 7,
  };
  const newMap = {};
  Object.keys(obj)
    .sort((a, b) => {
      return sorter[a] - sorter[b];
    })
    .forEach((item) => {
      newMap[item] = weekOverview[item];
    });
  return newMap;
};

console.log(sortByDays(weekOverview))

Better solution:

const weekOverview = {
    Sunday: 0.1111111111112,
    Monday: 0.1111111111111,
    Tuesday: 0.1100204936741015,
    Wednesday: 0.8655145400508841,
    Thursday: 0.2206230378805767,
    Friday: 0.7633485206232166,
    Saturday: 1.6047637274510986,
};

const sortWeek = (obj) => {
    const week = {
        Monday: null,
        Tuesday: null,
        Wednesday: null,
        Thursday: null,
        Friday: null,
        Saturday: null,
        Sunday: null,
    };

    for (const [key, value] of Object.entries(week)) {
        if (obj[key]) week[key] = obj[key];
    }

    return week;
};

console.log(sortWeek(weekOverview));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!