Sorting Dates in Node.js

I’m pretty new to javascript, and I’m working on a dashboard in which I want to display all of the dates since the first order was created. I have created the code that I need to add the dates for orders where the orders have not been created (with orders as 0), but now I’m having an issue where the dates that have orders created are listed in the beginning of the graph instead of in chronological order with the rest of the dates in the graph. I think this is a sorting issue, but I’m not sure, and my attempts to sort the date haven’t really made any changes to the graph. I would really appreciate any help or advice on how to fix this issue.

enter image description here

import express from 'express';
import expressAsyncHandler from 'express-async-handler';
import Order from '../models/orderModel.js';
import User from '../models/userModel.js';
import Product from '../models/productModel.js';
import {isAdmin, isAuth, isSellerOrAdmin, mailer, payOrderEmailTemplate} from '../utils.js';

const orderRouter = express.Router();
orderRouter.get(
  '/summary',
  isAuth,
  isAdmin,
  expressAsyncHandler(async (req, res) => {
function getDates(startDate, stopDate) {
  var dateArray = new Array();
  var currentDate = new Date(startDate);
  while (currentDate.getTime() <= new Date(stopDate).getTime()) {
      dateArray.push(getFormattedDate(currentDate));
      currentDate.setDate(currentDate.getDate() + 1)
      dateArray.sort((startDate, stopDate) => new Date(stopDate[0]).getTime() - new  Date(startDate[0]).getTime());
  }
  return dateArray;
}
  
    const dailySales = await Order.aggregate([
      {
        $group: {
          _id: { $dateToString: { format: '%m-%d-%Y', date: '$createdAt' } },
          orders: { $sum: 1 },
          sales: { $sum: '$totalPrice' },
          date: {$first: '$createdAt'}, 
        },
      },
      { $sort: { date: 1 } },
    ]);
  
  
    let today = new Date();

    let date=parseInt(today.getMonth()+1)+ "-"+ today.getDate()+"-"+today.getFullYear();
    
    
    const datesArray = getDates(dailySales[0]._id, date)
    
    
    for(let dateVal of datesArray){
      
     let isInArray = false;
     
     for(let dayVal of dailySales){
  
      
       if(dayVal._id === dateVal){
         isInArray = true;
       }
   
     }
       if(isInArray == false){
        dailySales.push({ "_id":dateVal, "orders":0, "sales":0}) 
      }
   }
res.send({dailySales});
  })
);