Both console.log show the same array, because when you use console.log(sortable) , sortable is passed by reference, and the console exits after your script completes - when sortable already sorted.
Simple code:
var arr = [3,2,1]; console.log(arr); // Produces `[1,2,3]` because its printed // to the console after `arr.sort();` arr.sort(); console.log(arr); // Produces `[1,2,3]`, as expected
Demo : http://jsfiddle.net/Rfwph/
Bypass
If you want to make console.log with an array to see it before changing, you can use .slice(0) to copy the array, i.e. to get an array of another that contains the same elements as your array.
var arr = [3,2,1]; console.log(arr.slice(0));
Demo : http://jsfiddle.net/Rfwph/2/
Edit: It is better to use .slice(0) instead of .slice() , which is supported in FF, but the ecma262 specification says that the end argument is optional.
Orientol
source share