Home > Software engineering >  Compare the time data in an array with current date in reactJS
Compare the time data in an array with current date in reactJS

Time:08-08

I need to sort an array with respect to the current time.

I defined a static array, and sorted it.Then I sorted the sorted array according to the current time, but I'm not getting the exact correct array.

// get the current date
let currentDate = new Date(Date.now());
// format the current date to be as same as the date in the array (e.g 07:00 AM)
let formattedCurrentDate = moment(currentDate).format('HH:MM ');
let trips = [
'14:50 ',
'15:18 ',
'18:20 ',
'15:48 ',
'07:00 ',
'09:40 ',
'08:45 ',
'18:00 ',
'16:08 ',
'16:18 ',
'17:18 ',
'10:00 ',
  ] as any;



//I used the insertion sort to sort the array

function sortedTrips(arr: any) {
// Start from the second element.
for (let i = 1; i < arr.length; i  ) {
// Go through the elements behind it.
for (let j = i - 1; j > -1; j--) {
// Value comparison with the formatted current Date
if (arr[j   1] > arr[j]) {
//  console.log('array time is less than Current Date', arr[j]);
// Swap
          [arr[j   1], arr[j]] = [arr[j], arr[j   1]];
        } else {
// console.log('array time is greater than Current Date', arr[j]);
        }
      }
    }
return arr;
  }

const sortedArray = sortedTrips(trips);
console.log('sortedArray', sortedArray);

//Then I sorted the sorted array according to the current time

function sortedCurrentTrips(arr: any) {
for (let i = 1; i < arr.length; i  ) {
// Go through the elements behind it.
for (let j = i - 1; j > -1; j--) {
// Value comparison with the formatted current Date
if (arr[j   1] > formattedCurrentDate) {
//  console.log('array time is less than Current Date', arr[j]);
// Swap
          [arr[j   1], arr[j]] = [arr[j], arr[j   1]];
        } else {
// console.log('array time is greater than Current Date', arr[j]);
        }
      }
    }
return arr;
  }
console.log('Final result', sortedCurrentTrips(sortedArray));

Final Result

LOG sorted Array ["18:20 ", "18:00 ", "17:18 ", "16:18 ", "16:08 ", "15:48 ", "15:18 ", "14:50 ", "10:00

", "09:40 ", "08:45 ", "07:00 "]

LOG Final result ["16:18 ", "17:18 ", "18:00 ", "18:20 ", "14:50 ", "15:18 ", "15:48 ", "16:08 ", "10:00 ", "09:40 ", "08:45 ", "07:00 "]

The expected result should be for time : 14:51 PM

Final result [ "15:18 ", "15:48 ","16:08 ", "16:18 ", "17:18 ", "18:00 ", "18:20 ", "14:50 ", "10:00 ", "09:40 ", "08:45 ", "07:00 "]

CodePudding user response:

You can use JavaScript's sort method.

const trips = [
  '14:50 ',
  '15:18 ',
  '18:20 ',
  '15:48 ',
  '07:00 ',
  '09:40 ',
  '08:45 ',
  '18:00 ',
  '16:08 ',
  '16:18 ',
  '17:18 ',
  '10:00 ',
];

const t = '14:51 ';

// sort without mutating original array
const tripsSorted = [...trips].sort();

// get all the trips before t and reverse sort
const arr1 = tripsSorted.filter((trip) => trip < t).reverse();

// get all the trips on or after t
const arr2 = tripsSorted.filter((trip) => trip >= t);

// concat
const result = arr2.concat(arr1);

console.log(result);

Result

['15:18 ', '15:48 ', '16:08 ', '16:18 ', '17:18 ', '18:00 ', '18:20 ', '14:50 ', '10:00 ', '09:40 ', '08:45 ', '07:00 ']
  • Related