Search code examples
javascriptjsonjavascript-objects

Trying to the sort the time in descending order


I'm trying to sort the time exited cars in descending order. Example:

This's just a part of the data I'm using. The actual data includes more than 100 data.

I tried using .sort((a, b) => b - a) on, let outDate = new Date(car.out)(this probably didn't work because it was sorting the dates as well, not sure) , hoursOut.sort((a, b) => b - a) this didn't work either. Received not a function error

Thanks in advance!




function displayData(data) {

  data.forEach(car =>{
    // -------------------------------------------- Time in --------------
    let dateIn = new Date(car.in)
    let month = dateIn.getMonth()
    let date = dateIn.getDate();
    let fullYear = dateIn.getFullYear()
    let hoursIn = dateIn.getHours();
    let minutesIn = dateIn.getMinutes();
    let secondsIn = dateIn.getSeconds()

    let timeInFormat = hoursIn >= 12 ? 'PM' : 'AM';

    let timeIn = month + '/' + date  + '/' + fullYear + ' ' + hoursIn + ':' + minutesIn + ':' + secondsIn + " " + timeInFormat

//input: 1591855348009
//output: 5/11/2020 16:47:48 PM

//-------------------------------------------------- Time out --------------------------------------------
    // let x = car.out
    // let carsOut = x.sort((a, b) => b - a)
    let outDate = new Date(car.out);
    // let sortedList = outDate.sort((a, b) => b.date - a.date)
    let monthOut = outDate.getMonth()
    let dateOut = outDate.getDate();
    let yearOut = outDate.getFullYear()
    let hoursOut = outDate.getHours();
    let minutesOut = outDate.getMinutes();
    let secondsOut = outDate.getSeconds()

    let timeOutFormat = hoursOut >= 12 ? 'PM' : 'AM';

    let timeOut = monthOut + '/' + dateOut  + '/' + yearOut + ' ' + hoursOut + ':' + minutesOut + ':' + secondsOut + " " + timeOutFormat


// ------------------------------------------ Duration

    let duration = ((Math.abs(dateIn - outDate))/3600000).toFixed(2)

    if (duration > 1) {
      var price = (duration * 2.99).toFixed(2);
    }else if (duration >= 24) {
      // change the font color to red
    }
    else{
      var price = 0;
       // change the font color to blue
      price.style.fontcolor = 'blue'    // not working
      // price.fontcolor("blue");
    }

    table.innerHTML += `<tr id=${car.id}>
                              <td>${car.license}</td>
                              <td>${price}</td>
                              <td>${duration}</td>        
                              <td>${timeIn}</td>
                              <td>${timeOut}</td>
                            </tr>`
    // table.innerHTML += row
  });

}

Solution

  • @Edit

      data.sort((a, b) => b.out - a.out).forEach(car =>{
        // -------------------------------------------- Time in --------------
        let dateIn = new Date(car.in)
        let month = dateIn.getMonth()
        let date = dateIn.getDate();
        let fullYear = dateIn.getFullYear()
        let hoursIn = dateIn.getHours();
        let minutesIn = dateIn.getMinutes();
        let secondsIn = dateIn.getSeconds()
    
        let timeInFormat = hoursIn >= 12 ? 'PM' : 'AM';
        hoursIn = hoursIn % 12
    
        let timeIn = month + '/' + date  + '/' + fullYear + ' ' + hoursIn + ':' + minutesIn + ':' + secondsIn + " " + timeInFormat
    
    //input: 1591855348009
    //output: 5/11/2020 16:47:48 PM
    
    //-------------------------------------------------- Time out --------------------------------------------
        // let x = car.out
        // let carsOut = x.sort((a, b) => b - a)
        let outDate = new Date(car.out);
        // let sortedList = outDate.sort((a, b) => b.date - a.date)
        let monthOut = outDate.getMonth()
        let dateOut = outDate.getDate();
        let yearOut = outDate.getFullYear()
        let hoursOut = outDate.getHours();
        let minutesOut = outDate.getMinutes();
        let secondsOut = outDate.getSeconds()
    
        let timeOutFormat = hoursOut >= 12 ? 'PM' : 'AM';
    
        let timeOut = monthOut + '/' + dateOut  + '/' + yearOut + ' ' + hoursOut + ':' + minutesOut + ':' + secondsOut + " " + timeOutFormat
    
    //input: 1591855348009
    //output: 5/11/2020 16:47:48 PM
    
    // ------------------------------------------ Duration
    
        let duration = ((Math.abs(dateIn - outDate))/3600000).toFixed(2)
    
        if (duration > 1) {
          var price = (duration * 2.99).toFixed(2);
        }else if (duration >= 24) {
          // change the font color to red
        }
        else{
          var price = 0;
           // change the font color to blue
          price.style.fontcolor = 'blue'    // not working
          // price.fontcolor("blue");
        }
    
        table.innerHTML += `<tr id=${car.id}>
                                  <td>${car.license}</td>
                                  <td>${price}</td>
                                  <td>${duration}</td>        
                                  <td>${timeIn}</td>
                                  <td>${timeOut}</td>
                                </tr>`
        // table.innerHTML += row
      });
    
    }