Sorting objects according to days name JavaScript

Let's say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday.

Sample Data

Following is our array:

const weather = [{
    day: 'Wednesday',
    humidity: 60
}, {
    day: 'Saturday',
    humidity: 50
}, {
    day: 'Thursday',
    humidity: 65
}, {
    day: 'Monday',
    humidity: 40
}, {
    day: 'Sunday',
    humidity: 35
}, {
    day: 'Friday',
    humidity: 80
}, {
    day: 'Tuesday',
    humidity: 45
}];

console.log("Original array:");
console.log(weather);
Original array:
[
  { day: 'Wednesday', humidity: 60 },
  { day: 'Saturday', humidity: 50 },
  { day: 'Thursday', humidity: 65 },
  { day: 'Monday', humidity: 40 },
  { day: 'Sunday', humidity: 35 },
  { day: 'Friday', humidity: 80 },
  { day: 'Tuesday', humidity: 45 }
]

Creating the Day Order Map

The key to this problem is creating our map object that maps the specific days of the week to their correct indices:

const dayOrder = {
    'Monday': 1,
    'Tuesday': 2,
    'Wednesday': 3,
    'Thursday': 4,
    'Friday': 5,
    'Saturday': 6,
    'Sunday': 7
};

console.log("Day order mapping:");
console.log(dayOrder);
Day order mapping:
{
  Monday: 1,
  Tuesday: 2,
  Wednesday: 3,
  Thursday: 4,
  Friday: 5,
  Saturday: 6,
  Sunday: 7
}

Sorting Objects by Day Names

Now we can apply the Array.prototype.sort() method passing in a custom callback that ranks the objects according to their index in the day order map:

const weather = [{
    day: 'Wednesday',
    humidity: 60
}, {
    day: 'Saturday',
    humidity: 50
}, {
    day: 'Thursday',
    humidity: 65
}, {
    day: 'Monday',
    humidity: 40
}, {
    day: 'Sunday',
    humidity: 35
}, {
    day: 'Friday',
    humidity: 80
}, {
    day: 'Tuesday',
    humidity: 45
}];

const dayOrder = {
    'Monday': 1,
    'Tuesday': 2,
    'Wednesday': 3,
    'Thursday': 4,
    'Friday': 5,
    'Saturday': 6,
    'Sunday': 7
};

weather.sort((a, b) => {
    return dayOrder[a.day] - dayOrder[b.day];
});

console.log("Sorted array:");
console.log(weather);
Sorted array:
[
  { day: 'Monday', humidity: 40 },
  { day: 'Tuesday', humidity: 45 },
  { day: 'Wednesday', humidity: 60 },
  { day: 'Thursday', humidity: 65 },
  { day: 'Friday', humidity: 80 },
  { day: 'Saturday', humidity: 50 },
  { day: 'Sunday', humidity: 35 }
]

How It Works

The sort method compares two objects at a time using the custom comparison function. When we return dayOrder[a.day] - dayOrder[b.day], we're subtracting the numeric values assigned to each day. If the result is negative, object 'a' comes before 'b'; if positive, 'b' comes before 'a'; if zero, they remain in their current order.

Alternative Approach Using Array of Days

You can also use an array of days and the indexOf() method:

const weather = [{
    day: 'Wednesday',
    humidity: 60
}, {
    day: 'Saturday', 
    humidity: 50
}, {
    day: 'Monday',
    humidity: 40
}];

const daysOrder = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

weather.sort((a, b) => {
    return daysOrder.indexOf(a.day) - daysOrder.indexOf(b.day);
});

console.log(weather);
[
  { day: 'Monday', humidity: 40 },
  { day: 'Wednesday', humidity: 60 },
  { day: 'Saturday', humidity: 50 }
]

Conclusion

Sorting objects by day names requires mapping each day to a numeric value that represents the correct order. The map approach is more efficient than using indexOf() for larger datasets, as it provides O(1) lookup time.

Updated on: 2026-03-15T23:18:59+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements