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.
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:
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:
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.
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?
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:
The comparator stays the same. Only the domain rule changes.
Sort Related Objects by Day
Often the array contains objects rather than raw strings. The same idea still applies:
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
indexOfcalls. - 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
- Sort array with with first half and second half sorted
- Sort ArrayList of custom Objects by property
- Sort BST in On using constant memory
- Sort Dictionary by keys
- sort outer array based on values in inner array, javascript
- Sort points in clockwise order?
- Sort JavaScript object by key
- Sorting Array with JavaScript reduce function

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 courseTrack 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.