JavaScript
Array Sorting
Days of the Week
Programming
Web Development

Sort array of days in javascript

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Sorting day names in JavaScript is not a normal alphabetical sort. If you call sort() directly on values such as "Tuesday", "Monday", and "Sunday", JavaScript orders them as strings, not in calendar order. The fix is to define the intended weekday order and compare each item by that custom ranking.

Define the Week Order Explicitly

The most direct solution is to keep a reference array and sort by the index of each day:

javascript
1const weekOrder = [
2  "Monday",
3  "Tuesday",
4  "Wednesday",
5  "Thursday",
6  "Friday",
7  "Saturday",
8  "Sunday"
9];
10
11const days = ["Sunday", "Tuesday", "Monday", "Friday"];
12
13days.sort((a, b) => weekOrder.indexOf(a) - weekOrder.indexOf(b));
14console.log(days);

This works because each day name is mapped to its position in the intended ordering.

For short arrays this is perfectly fine. For frequent sorting or larger inputs, a lookup map is cleaner and avoids repeated indexOf calls.

Use a Lookup Map for Better Comparisons

A mapping object or Map gives you direct ranking values:

javascript
1const order = {
2  Monday: 0,
3  Tuesday: 1,
4  Wednesday: 2,
5  Thursday: 3,
6  Friday: 4,
7  Saturday: 5,
8  Sunday: 6
9};
10
11const days = ["Sunday", "Tuesday", "Monday", "Friday"];
12const sorted = [...days].sort((a, b) => order[a] - order[b]);
13
14console.log(sorted);

The spread operator copies the array before sorting. That matters because sort() mutates the original array.

Normalize Input Before Sorting

Real inputs are not always clean. You may receive lowercase names, abbreviations, or extra whitespace. Normalize the data before comparing it.

javascript
1const order = {
2  monday: 0,
3  tuesday: 1,
4  wednesday: 2,
5  thursday: 3,
6  friday: 4,
7  saturday: 5,
8  sunday: 6
9};
10
11const days = [" sunday ", "Tuesday", "monday", "Friday"];
12
13const sorted = [...days].sort((a, b) => {
14  const left = order[a.trim().toLowerCase()];
15  const right = order[b.trim().toLowerCase()];
16  return left - right;
17});
18
19console.log(sorted);

Normalization prevents small formatting differences from breaking the comparator.

Decide What to Do With Invalid Values

If the array may contain unexpected values, decide what should happen before you sort. Should invalid values be removed, pushed to the end, or treated as an error?

javascript
1const order = {
2  monday: 0,
3  tuesday: 1,
4  wednesday: 2,
5  thursday: 3,
6  friday: 4,
7  saturday: 5,
8  sunday: 6
9};
10
11const days = ["Monday", "Holiday", "Sunday", "Tuesday"];
12
13const sorted = [...days].sort((a, b) => {
14  const left = order[a.toLowerCase()] ?? Number.MAX_SAFE_INTEGER;
15  const right = order[b.toLowerCase()] ?? Number.MAX_SAFE_INTEGER;
16  return left - right;
17});
18
19console.log(sorted);

This version pushes unknown values to the end instead of producing unreliable comparisons.

Week Start Is a Business Rule

Not every app wants Monday first. Some calendars and scheduling systems treat Sunday as the beginning of the week. In that case, change the reference order rather than changing the sort logic:

javascript
1const weekOrder = [
2  "Sunday",
3  "Monday",
4  "Tuesday",
5  "Wednesday",
6  "Thursday",
7  "Friday",
8  "Saturday"
9];

The comparator stays the same. Only the domain rule changes.

Often the array contains objects rather than raw strings. The same idea still applies:

javascript
1const order = {
2  Monday: 0,
3  Tuesday: 1,
4  Wednesday: 2,
5  Thursday: 3,
6  Friday: 4,
7  Saturday: 5,
8  Sunday: 6
9};
10
11const tasks = [
12  { day: "Friday", task: "Deploy" },
13  { day: "Monday", task: "Plan" },
14  { day: "Wednesday", task: "Review" }
15];
16
17tasks.sort((a, b) => order[a.day] - order[b.day]);
18console.log(tasks);

This is usually the pattern you want in real application code.

Common Pitfalls

The biggest mistake is calling sort() with no comparator and expecting weekday order. JavaScript only knows string ordering unless you teach it the calendar order.

Another mistake is forgetting that sort() mutates the original array. If other parts of the program rely on the original order, sort a copy instead.

People also ignore invalid inputs. A comparator that returns undefined - undefined is not a stable plan.

Finally, do not hard-code Monday-first unless that is truly the rule for the product. Week order is often a domain choice, not a universal truth.

Summary

  • Day names need a custom comparator because alphabetical order is not calendar order.
  • A lookup map is usually cleaner than repeated indexOf calls.
  • Normalize case and whitespace when the input may be inconsistent.
  • Decide explicitly how invalid values should be handled.
  • Change the reference order, not the comparator logic, when the week should start on a different day.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.