0

I would like to sort dates according to each id and when an id is passed it will appear in a "passed id" section, if it is in progress in a "current" section and if it is not in progress passed in a "coming soon" section. Start and end have this format: start: '2021-04-26T13:00:00.000Z', end: '2021-04-26T15:30:00.000Z'

Thank you !

here is my code :

fetch(urlCourses)
  .then(res => res.json())
  .then(function (data) {

    let donnees = data;

    //console.log( JSON.stringify(tabStudents));
  donnees.forEach(e => {
      tabCourses.push({
        'id': e.id,
        'name': e.name,
        'start': e.start,
        'end': e.end,
        'school_id': e.school_id,
       
        //'students': e.STUDENTS,
        'classroom': e.classroom
      });
    });
   
    tabCourses.sort(function (a, b) {
      return new Date(a.start) - new Date(b.start);
    });
    
   console.log('tabCourses sorted', tabCourses);

  
  })
  .catch(function (error) {
    console.log(error);
  });
rabiya
  • 9
  • 1

2 Answers2

0

If you want to sort by a column and then by another column, I can refer you to this answer. (the brilliant one) sort Javascript array by two numeric fields

Should you want to sort into 3 distinct lists according to the value of dates (start, end and current) then first you want to run over the array, adding a column named "section" with possible values of : "passed", "progress", "future".

Then you can sort by it or separate it into 3 lists.

I leave it to you or others to write the code for that.

IT goldman
  • 14,885
  • 2
  • 14
  • 28
0

Convert your date in timestamp and it will do the trick

    tabCourses.sort(function (a, b) {
  return new Date(a.start).getTime() - new Date(b.start).getTime();
});
Konflex
  • 465
  • 3
  • 11