- Home /
Swapping elements of an array
Hello, how can I swap 2 elements of an array (the index)? The code I am using now is:
var temp = kartsRankingArray[o];
kartsRankingArray[o] = kartsRankingArray[o+1];
kartsRankingArray[o+1] = temp;
It works, but I am not sure. Won't this swap the contents of the objects instead of swapping the indexes of the objects in the array?
Thanks!
Leo ;)
Comment
Best Answer
Answer by jahroy · Dec 17, 2011 at 07:18 AM
That's the most straightforward way of doing it.
When they teach freshmen computer science students how to sort an array, they start with something called a bubble sort.
The code looks just like that!