I was writing a simple code exercise solution and came across what appears to be object.entries()
doing an internal numeric ascending sort. I was expecting to have to sort the key value pairs myself since the docs say that you must do a sort if you need a specific order. Why is this is happening and can I rely on this behavior?
/*
Given an input of [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20],
make a function that organizes these into individual array that is ordered.
The above input should return:
[[1,1,1,1],[2,2,2],4,5,10,[20,20],391,392,591].
*/
const cleanTheRoom = (mess) => {
let hash = {};
for (let i = 0; i < mess.length; i++) {
(hash.hasOwnProperty(mess[i])) ? hash[mess[i]]++ : hash[mess[i]] = 1;
}
const clean = [];
for (const [key, value] of Object.entries(hash)) {
(value > 1) ? clean.push(Array(value).fill(key)) : clean.push(key);
}
return clean;
}
let cleaned = cleanTheRoom(
[1,2,4,591,392,391,2,5,10,2,1,1,1,20,20]
);
console.log(cleaned);