Code: Select all
C% = DIM(key(),1) + 1
CALL Sort%, key(0), dat(0)
So the above code won't leave the data elements unaffected if the keys are identical, it will sort them according to their values. How can we achieve the desired behaviour? The answer is to introduce an additional, dummy, array which determines the order when the keys are identical:
Code: Select all
DIM dummy%(DIM(key().1))
FOR I% = 0 TO DIM(dummy%(),1) : dummy%(I%) = I% : NEXT
C% = DIM(key(),1) + 1
CALL Sort%, key(0), dummy%(0), dat(0)
Exercise: If we want to do a descending sort rather than an ascending sort, but still keep the order of the data elements the same when the keys are identical, what change do we need to make to this code?