Sort array by month-year JavaScript

Suppose, we have an array that contains dates in MM-YYYY format like this ?

const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];
console.log("Original array:", arr);
Original array: [
  '1-2016',  '7-2015',  '7-2016',
  '3-2016',  '8-2016',  '2-2016',
  '6-2016',  '8-2015',  '5-2016',
  '4-2016',  '9-2015',  '10-2015',
  '11-2015', '12-2015'
]

We are required to write a JavaScript function that takes in one such array and sorts it such that the dates in the array are arranged in oldest to newest order.

Using Custom Sort Function

The approach involves creating a comparison function that converts MM-YYYY strings into comparable formats. We pad single-digit months with zero and combine year with month for proper comparison.

const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];

const padToString = (num) => {
    return String("0" + num).slice(-2);
};

const sortByDate = (first, second) => {
    const firstPart = first.split('-');
    const secondPart = second.split('-');
    
    // Create YYYYMM format for comparison
    const a = firstPart[1] + padToString(firstPart[0]);
    const b = secondPart[1] + padToString(secondPart[0]);
    
    return a - b;
};

arr.sort(sortByDate);
console.log("Sorted array:", arr);
Sorted array: [
  '7-2015',  '8-2015',  '9-2015',
  '10-2015', '11-2015', '12-2015',
  '1-2016',  '2-2016',  '3-2016',
  '4-2016',  '5-2016',  '6-2016',
  '7-2016',  '8-2016'
]

How It Works

The padToString function ensures single-digit months are zero-padded (1 becomes "01"). The sortByDate function splits each date string, creates a YYYYMM format, and performs numeric comparison for proper chronological ordering.

Alternative Method Using Date Objects

const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016"];

const sortByDateObjects = (a, b) => {
    const [monthA, yearA] = a.split('-');
    const [monthB, yearB] = b.split('-');
    
    const dateA = new Date(yearA, monthA - 1); // Month is 0-indexed
    const dateB = new Date(yearB, monthB - 1);
    
    return dateA - dateB;
};

const sortedArr = [...arr].sort(sortByDateObjects);
console.log("Sorted using Date objects:", sortedArr);
Sorted using Date objects: [ '7-2015', '1-2016', '2-2016', '3-2016', '6-2016', '7-2016' ]

Comparison

Method Performance Readability Use Case
Custom String Comparison Faster Good Simple MM-YYYY formats
Date Objects Slower Better Complex date operations

Conclusion

Both methods effectively sort MM-YYYY date strings chronologically. The custom string comparison is more efficient for simple sorting, while Date objects offer better readability and extensibility for complex date operations.

Updated on: 2026-03-15T23:19:00+05:30

764 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements