Here is a example of what I want to do with NSArray contains NSNumber.
-
This is the NSArray “score” I want to edit.
score[0]=30,
score[1]=10,
score[2]=20
score[3]=0 -
Sort the Array in ascending-order
score[0]=30 //[0]This number shows the index before sorting the array
score[1]=20 //[2]
score[2]=10 //[1]
score[3]=0 //[3] -
Edit the Array(In this case,4th gives 1st 10 points,and 3rd gives 2nd 5points)
score[0]=40 //[0]
score[1]=25 //[2]
score[2]=5 //[1]
score[3]=-10 //[3] -
And sort the array back they were.
score[0]=40
score[1]=5
score[2]=25
score[3]=-10I have a problem with no.4 method in the list.Can someone give me some idea with this?
Thanks in advance.
You don’t actually need to swap the values themselves, you can set up an extra level of indirection and use that.
Before the sort, you have the indexes initialised to point to the corresponding scores:
When you sort, you actually sort the indexes based on the scores they point to rather than the scores themselves. So, instead of the following comparison in your sort:
you instead use:
Following the sort, you then have:
Then, to move points, you use the indirect indexes rather than the direct values:
Then you throw away the indexes altogether, the original array doesn’t need restoration to its original order, simply because its order was never changed.