a = [3, 1, 2]; a.sort(); console.log(a);
[1, 2, 3]
a = [3, 1, 2]; function b(v1, v2){ return v2-v1; }; a.sort(b); console.log(a);
[3, 2, 1]
a = [3, 1, 2]; function b(v1, v2){ return v1-v2; }; a.sort(b); console.log(a);
[1, 2, 3]
a = [3, 1, 2]; function b(v1, v2){ return 0; }; a.sort(b); console.log(a);
[3, 1, 2]
a = [3, 1, 2]; function b(v1, v2){ console.log('c', v1, v2); return 0; }; a.sort(b); console.log(a);
c 3 1
c 1 2
[3, 1, 2]
a = [3, 1, 2]; a.sort(function(v1, v2){return v2-v1}); console.log(a); // 익명함수
[3, 2, 1]