JavaScript Array sort Function
Developments/JavaScript/Html / 2013. 6. 17. 16:13
javascript array sort function
ex ) 오름차순
1. default
var array = [46,13,5,76,4,78,99,41,755,6]; array.sort(function (left,right){ return left-right; }); alert(array);
2. 직접만들어본 오름차순 sort function
var array = [46,13,5,76,4,78,99,41,755,6]; function ArraySortFn(arr){ for(var i=0; i<arr.length;i++){ for(var j=i+1; j<arr.length;j++){ var compare1=arr[i]; var compare2=arr[j]; if(compare1>compare2){ arr[i] = compare2; arr[j] = compare1; } } } } alert(ArraySortFn(array));
결과는 동일하다.
결과값 : [4, 5, 6, 13, 41, 46, 76, 78, 99, 755]