JS: Problems with the order of a Dates array

Asked

Viewed 23 times

1

Hello!

I’m having trouble in Javascript with the order of an array. Basically, I take a list of objects and sort it according to one of its attributes which is a Date. But when I give a console.log, when used in a function or when placed in a . txt, different results appear and the array is partially ordered.

Specifying:

1 - I sort the array as follows:

commands.sort(compareDate)

function compareDate (command1, command2) {
  if (command1.date.getTime() < command2.date.getTime()) {
    return -1
  }
  if (command1.date.getTime() > command2.date.getTime()) {
    return 1
  }
  return 0
}

Then if I print it by console.log, everything seems orderly (attention in the schedules):

[ { date: 2019-06-02T16:26:50.000Z, button: 'NET_VOLUMEUP' },
  { date: 2019-06-02T16:26:51.000Z, button: 'NET_VOLUMEUP' },
  { date: 2019-06-02T16:26:52.000Z, button: 'NET_VOLUMEDOWN' },
  { date: 2019-06-02T16:26:54.000Z, button: 'NET_VOLUMEUP' },
  { date: 2019-06-02T16:26:56.000Z, button: 'NET_VOLUMEDOWN' },
  { date: 2019-06-02T16:26:57.000Z, button: 'NET_VOLUMEDOWN' },
  { date: 2019-06-02T16:26:59.000Z, button: 'NET_VOLUMEUP' },
...

Then I wanted to put it all in a . txt, so I did:

for (let i = 0; i < commands.length; i++) {
  write(commands[i].date + ',' + commands[i].button)
}

function write (text) {
  fs.appendFile('logs/parsed.txt', text + '\r\n', function (err) {
    if (err) {
      return console.log(err)
    }
  })
}

What generates me a result already out of order (attention in the schedules and ignore the zone):

Sun Jun 02 2019 13:26:52 GMT-0300 (GMT-03:00),NET_VOLUMEDOWN
Sun Jun 02 2019 13:26:51 GMT-0300 (GMT-03:00),NET_VOLUMEUP
Sun Jun 02 2019 13:26:54 GMT-0300 (GMT-03:00),NET_VOLUMEUP
Sun Jun 02 2019 13:26:57 GMT-0300 (GMT-03:00),NET_VOLUMEDOWN
Sun Jun 02 2019 13:26:56 GMT-0300 (GMT-03:00),NET_VOLUMEDOWN
Sun Jun 02 2019 13:26:59 GMT-0300 (GMT-03:00),NET_VOLUMEUP
Sun Jun 02 2019 13:27:00 GMT-0300 (GMT-03:00),NET_VOLUMEUP
Sun Jun 02 2019 13:27:00 GMT-0300 (GMT-03:00),NET_VOLUMEDOWN
Sun Jun 02 2019 13:27:01 GMT-0300 (GMT-03:00),NET_VOLUMEUP
Sun Jun 02 2019 13:27:01 GMT-0300 (GMT-03:00),NET_VOLUMEDOWN
Sun Jun 02 2019 13:27:02 GMT-0300 (GMT-03:00),NET_VOLUMEUP
Sun Jun 02 2019 13:27:03 GMT-0300 (GMT-03:00),NET_VOLUMEDOWN
Sun Jun 02 2019 13:27:03 GMT-0300 (GMT-03:00),NET_VOLUMEUP
Sun Jun 02 2019 13:26:50 GMT-0300 (GMT-03:00),NET_VOLUMEUP
Sun Jun 02 2019 15:39:24 GMT-0300 (GMT-03:00),SAM_CENTER
Sun Jun 02 2019 15:38:12 GMT-0300 (GMT-03:00),SAM_POWER
Sun Jun 02 2019 15:38:12 GMT-0300 (GMT-03:00),SAM_POWER

I tried to use too date.toString(), date.toUTCString() and date.toLocaleString(), which also generated results out of order. Worse than that, the results do not seem to follow a pattern. Now the second object that is out of order, now the third object, and so it goes.

I’ve considered the fact that this could be a function problem fs.appendFile, but the problem is when I traverse this array into another function and consume its objects, they also appear with some out-of-order indexes, undermining the functioning of my code.

Any ideas? Any help?

  • The function appendFile is asynchronous, if you call her again before she finishes writing, as you are doing in the for, it is possible that your array will be written out of order even. Why not send your text at once instead of calling io several times?

  • Perfect! I thought it might be something about asynchronous functions, but I hadn’t thought of a solution as simple as calling the appendFile already with the text all assembled. Thank you!

  • Can you use this jsFiddle until you play the problem? https://jsfiddle.net/pkzxyf1a/

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.