Hello again! I seem to be coming back here more and more with this latest project.
I'm trying to sort an array of objects based on one property of the objects. All objects in the array have this property, but this property is not always a number. The sort function I supply DOES convert the property into a number for the sort. I only mention it at all in case it's somehow part of the problem.
The problem is that the resulting array is NOT sorted. I added extra code inside the function so I can double-check the comparison being made, and confirm that the correct values are being compared. But the final array is in the exact same order as the initial array, even though (in the specific case that got me checking) the final element should be the first according to my criteria.
I should also note that the array I'm sorting from is NOT the same as the array I'm sorting INTO, as I need the original array kept unmodified.
I admit that the sort function is something I'm just starting to use, so I don't fully understand everything about it. The routine works in all other instances I've tried so far, but in all those other instances, the original array meets the sort criteria from the start, hence why only one instance has caught my attention so far.
Here's the relevant code:
Thanks in advance!
I'm trying to sort an array of objects based on one property of the objects. All objects in the array have this property, but this property is not always a number. The sort function I supply DOES convert the property into a number for the sort. I only mention it at all in case it's somehow part of the problem.
The problem is that the resulting array is NOT sorted. I added extra code inside the function so I can double-check the comparison being made, and confirm that the correct values are being compared. But the final array is in the exact same order as the initial array, even though (in the specific case that got me checking) the final element should be the first according to my criteria.
I should also note that the array I'm sorting from is NOT the same as the array I'm sorting INTO, as I need the original array kept unmodified.
I admit that the sort function is something I'm just starting to use, so I don't fully understand everything about it. The routine works in all other instances I've tried so far, but in all those other instances, the original array meets the sort criteria from the start, hence why only one instance has caught my attention so far.
Here's the relevant code:
JavaScript:
let newPool = this.data.buttons.sort((a, b) => {
const x1 = this.getItemX(a), x2 = this.getItemX(b);
return x1 > x2;
})
Thanks in advance!