Grouping data to month wise in JavaScript


In the given problem statement we have to group the given data month wise. In simple terms the data should be in sorted form after grouping them month wise. For example we have years and months given in data so it should be visible in the order from January to December.

As we know that Arrays are the data structures template to store data. And we can manipulate that data as per our requirement. It can store a collection of items in a single array, and has many operations to perform like adding an element, deleting an element, or searching a particular element in the array .

Let us understand with example below which shows the problem statement in practical way:

const arr = [
{
   year: 2020,
   month: 'April'
}, {
   year: 2020,
   month: 'January'
}, {
   year: 2017,
   month: 'May'
}, {
   year: 2017,
   month: 'March'
}, ]

The above array should be shown in sorted form like this:

{ year: 2017, month: 'March' },
{ year: 2017, month: 'May' },
{ year: 2020, month: 'January' },
{ year: 2020, month: 'April' }

Algorithm

The algorithm mentioned below will give a step by step process to solve the given problem.

For example if we have given an array defining year and month, then we should provide its precise algorithm as follows:

Step 1: Declare an array with any data type let, var or const.

Step 2 : Declare another array called months that takes in all the months to store in it.

Step 3 : Create a function called sortedData which takes two parameters.

Step 4 : Check first if condition for year if condition is true then execute it otherwise goes to the else part.

Step 5 : Check the else condition with indexOf method which returns the first index at which a given element can be found in the array, or -1 if it is not present.

Step 6 : Once the comparison gets successful, declare the sort method. The sort() method sorts the present elements in an array. It also overwrites the actual array.

Step 7 : After the conditions get satisfied, it will print the sorted data in the console.

Example

// define data here in array form
const data = [{ name: 'Pearl',
   year: 2020,
   month: 'January'
}, {
   name: 'John',
   year: 2017,
   month: 'March'
}, {
   name: 'Peter',
   year: 2010,
   month: 'January'
}, {
   name: 'Nick',
   year: 2010,
   month: 'December'
},{
   name: 'Angel',
   year: 2020,
   month: 'October'
}, {
   name: 'Jas',
   year: 2017,
   month: 'June'
}];

const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

// create function to sort
const sortedData = (a, b) => {
   if(a.year !== b.year){
     return a.year - b.year;
   }
   else{
     // comparison of months at each index
     return months.indexOf(a.month) - months.indexOf(b.month);
   };
};

// using sort method in javascript 
data.sort(sortedData);
console.log(data);

Output

[
  { name: 'Peter', year: 2010, month: 'January' },
  { name: 'Nick', year: 2010, month: 'December' },
  { name: 'John', year: 2017, month: 'March' },
  { name: 'Jas', year: 2017, month: 'June' },
  { name: 'Pearl', year: 2020, month: 'January' },
  { name: 'Angel', year: 2020, month: 'October' }
]

In the above code we have declared an array which is storing data. Another array which is storing months. And we have declared a function named sortedData. Its work is to compare the values and give ordered data with sort() method.

And then we have defined another javascript method, indexOf. This method’s working mechanism is to return the position of the index of a value in the string. It also gives -1 if the value is not found. The indexOf() method uses camel case to define it.

The sort() method is used to arrange the elements of an array in some order, when you need to sort an array of objects by a certain criteria.

Complexity

For this program the time complexity is O(n log n). Here n is the number of objects in the data array. The time complexity of the sort() method is O(n log n). So we can say that the overall time complexity in this program is O(n log n). Space complexity for this program is O(n), to store all the elements of the array.

Conclusion

This is how we can solve the given problem statement and arrange data in a sorted form with the help of predefined methods sort() and indexOf() present in javascript. And the time complexity of this algorithm is O(n log n). So with the help of this program you can learn how to use sort and indexOf methods present in javascript.

Updated on: 18-Aug-2023

677 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements