Sorting an array behaves differently in Browser and Node.js environment when the comparator function returns boolean value instead of number.
e.g.
var x = [
{
"id": 1110000003977716,
"updatedAt": 1558353055000,
},
{
"id": 1,
"updatedAt": 1551783874000,
},
{
"id": 2,
"updatedAt": 1551788206000,
},
{
"id": 3,
"updatedAt": 1552066859000,
},
{
"id": 4,
"updatedAt": 1552578810000,
},
{
"id": 5,
"updatedAt": 1552598063000,
},
{
"id": 6,
"updatedAt": 1554906497000,
},
{
"id": 7,
"updatedAt": 1555061487000,
},
{
"id": 8,
"updatedAt": 1555357728000,
},
{
"id": 9,
"updatedAt": 1555616572000,
},
{
"id": 10,
"updatedAt": 1555698720000,
},
{
"id": 11,
"updatedAt": 1555843111000,
},
{
"id": 12,
"updatedAt": 1555851294000,
},
{
"id": 13,
"updatedAt": 1557408493000,
}
];
and sorting method
x.sort((p, n) => p.updatedAt < n.updatedAt)
Browser sort this array correctly,
Output
[
{
"id":1110000003977716,
"updatedAt":1558353055000
},
{
"id":13,
"updatedAt":1557408493000
},
{
"id":12,
"updatedAt":1555851294000
},
{
"id":11,
"updatedAt":1555843111000
},
{
"id":10,
"updatedAt":1555698720000
},
{
"id":9,
"updatedAt":1555616572000
},
{
"id":8,
"updatedAt":1555357728000
},
{
"id":7,
"updatedAt":1555061487000
},
{
"id":6,
"updatedAt":1554906497000
},
{
"id":5,
"updatedAt":1552598063000
},
{
"id":4,
"updatedAt":1552578810000
},
{
"id":3,
"updatedAt":1552066859000
},
{
"id":2,
"updatedAt":1551788206000
},
{
"id":1,
"updatedAt":1551783874000
}
]
But in node.js the result is different (sometimes correct);
Output
[
{
"id":7,
"updatedAt":1555061487000
},
{
"id":1110000003977716,
"updatedAt":1558353055000
},
{
"id":8,
"updatedAt":1555357728000
},
{
"id":13,
"updatedAt":1557408493000
},
{
"id":12,
"updatedAt":1555851294000
},
{
"id":11,
"updatedAt":1555843111000
},
{
"id":10,
"updatedAt":1555698720000
},
{
"id":9,
"updatedAt":1555616572000
},
{
"id":6,
"updatedAt":1554906497000
},
{
"id":5,
"updatedAt":1552598063000
},
{
"id":4,
"updatedAt":1552578810000
},
{
"id":3,
"updatedAt":1552066859000
},
{
"id":2,
"updatedAt":1551788206000
},
{
"id":1,
"updatedAt":1551783874000
}
]
Please explain why this behaviour is different in these two engines.
P.S. I have tested my code in Chrome and Firefox.