Another possible approach is to add an extra data member to the object, which holds the index within the array being sorted. Sort the array using an unstable sorting algorithm. Run a stabilization pass over the resulting array to fix the stability of all equal elements.
Other frustrated coders have reached this conclusion as well, suggesting implementing Merge Sort instead, which is a good ideas if you need a comparison based sorting algorithm.
Enjoy, and let me know if it’s helpful.
At times stability is not needed. Two such cases are where the array is all numbers, and where primary database keys are used for sorting by. In the first case, there is no difference between numbers, and thus it doesn’t matter if identical numbers are swapped, since it’s impossible to tell the difference between equal numbers. In the second case or sorting by primary database key, these keys are unique, without duplicates, and thus, equal keys will never occur.