var data = [3,5,6,9,7,8,4,3,2,1];
var str = '';
//array native forEach
data.forEach(function(e){
str += e + ',';
});
//print 3,5,6,9,7,8,4,3,2,1
console.log(str.substring(0,str.length-1));
Array Native Sort
var data = [3,5,6,9,7,8,4,3,2,1];
var str = '';
data.sort();
data.forEach(function(e){
str += e + ',';
});
//1,2,3,3,4,5,6,7,8,9
console.log(str.substring(0,str.length-1));
Customer Compare Funciton
var data = [3,5,6,9,7,8,4,3,2,1];
var str = '';
function compare(a,b){
return b - a;
}
data.sort(compare);
data.forEach(function(e){
str += e + ',';
});
//9,8,7,6,5,4,3,3,2,1
console.log(str.substring(0,str.length-1));
沒有留言:
張貼留言