I’m using array.sort to sort an array of objects.
Is there someway to determine if the order of those objects changed other than doing a comparison myself? Some in-built JS function? I don’t need to know the # of changes, just that there was at least 1 change.
Update:
I’m sorting custom objects, e.g.:
[
{ 'value' : 5, 'name' : 'foo'},
{ 'value' : 3, 'name' : 'bar'},
{ 'value' : 10, 'name' : 'js'}
]
Update 2:
I’m considering adding an additional custom property ‘order’ as I generate the objects and then checking to see if ‘order’ is or isn’t sequential after the .sort. Is there a faster/better method?
If you don’t want to mess with two copies of the array, check the array before starting the sort.
You can loop through the array and return true if every item is in its sorted position,
(compares ‘less’ than the next item) or false as soon as one item is detected in the wrong position.
Since you are using objects, the comparison code depends on the way you are sorting the objects.