I'm having trouble with sorting this array. The array contains objects with SortTime and Operation values like this:
array = [ {SortTime : 123456, Operation : Assigning}, {SortTime : 4567 , Operation: Assigning}, {SortTime : 123456 , Operation: Assigned} ];
When comparing array[0] and array[2], I want to maintain the order so that "Assigning" comes before "Assigned".
Each of these numbers is an epoch number, so I need the algorithm to preserve the original order if two numbers are equal (e.g., if array[4] = 12345 and array[5] = 12345, I don't want them to switch positions in the final order).
array.sort(function(a, b) {
if (a.val.SortTime === b.val.SortTime) {
return -1;
} else {
return a.val.SortTime - b.val.SortTime;
}
});
This approach isn't working because it often swaps positions of equal numbers. I apologize if my explanation is unclear. Thank you for your help!