I'm trying to sort an array, where its objects consist of a dynamic key of type number.
Now I want to sort the objects so that the numbers start from the smallest first -- the number I mean is the key of the object.
Here's my code
const arr = [{"3": 32}, {"1": 42}, {"5": 48}];
arr.sort((a,b) => {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
});
console.log(arr);
The order of the objects should be:
[{"1": 42},{"3":32},{"5":48}]