I am trying to reorder an array (moving one item of 90 to the top, and move all the others down).
Is there a way to do this?
I am trying to reorder an array (moving one item of 90 to the top, and move all the others down).
Is there a way to do this?
NSMutableArray* array;
...
id ninety = [array objectAtIndex:90];
[ninety retain]; // needs retain because -removeObject will release that object.
[array removeObjectAtIndex:90];
[array insertObject:ninety atIndex:0];
[ninety release];
You need to use an NSMutableArray. Remove the object you want to move with removeObjectAtIndex:
and then stick it back in its new location with insertObject:atIndex:
.
If it's a NSMutableArray
you can use -exchangeObjectAtIndex:withObjectAtIndex:
to swap item 0 and the one you want to move to the top. No need to mess with retain
and release
.
(Edited - this is not exactly what the questioner wanted - the reponse by KennyTM is the correct one for that case)
1) keep in mind item 90
2) move items 0..89 one slot down (can use func memmove())
3) put former '90' item in slot 0