I've been racking my brain trying to figure out a sorting logic for some arrays, but I'm stumped. Maybe you have an idea?
Imagine we have an array of objects structured like this:
let obj = {day:'2', month:'6', year:'1938' }
The array would look something like this:
let array = [{year:'1938, month:'6', day:'3'},{year:'1935', month:'5', day:'3'},{year:'1935, month:'', day:''}, {year:'1935', month:'5', day:''}, {year:'1934}, month:'3', day:''}, {year:'1934', month:'3', day:'15'}, {year:'1934}, month:'', day:''}];
I want the sorted array to look like this:
let sortedArray = [{year:'1934}, month:'', day:''},{year:'1934}, month:'3', day:''},{year:'1934}, month:'3', day:'15'},{year:'1935, month:'', day:''},{year:'1935', month:'5', day:''},{year:'1935', month:'5', day:'3'},{year:'1938, month:'6', day:'3'}
The year, month, and day fields are not required in my app, but I want to display them sorted chronologically starting with objects that only have the year, then those with year and month, and finally those with all three elements. This helps me build a timeline.
I tried creating 3 separate arrays - one for objects with only years, one for those with years and months, and another for objects with all fields filled.
My approach looked something like this:
// code snippet
Unfortunately, my logic hit a roadblock when I had to compare months between different arrays. If there's a solution to sort the array as described, your help would be greatly appreciated. Thank you!