Encountered an issue today, not sure if it's a coding problem or a bug in Javascript. Attempting to sort an object array structured like this:
const array = [{
text: 'one',
count: 5
},
{
text: 'two',
count: 5
},
{
text: 'three',
count: 5
},
{
text: 'four',
count: 5
}]
Attempting to sort the object array based on the count index, using the following code:
Array.prototype.sortBy = function (p) {
return this.slice(0).sort(function (a, b) {
return (a[p] > b[p]) ? 1 : (a[p] < b[p]) ? -1 : 0;
});
}
console.log(array.sortBy('count'))
Sorting works well for object arrays with less than 100 elements, but fails for larger arrays. Tried using some Npm packages as well without success. Any assistance would be appreciated.