一,reverse()元素顺序翻转
二,sort如果元素是字符串类型,那么数组的可变方法sort()就非常好使:
var names = ["David","Mike","Cynthia","Clayton","Bryan","Raymond"];
names.sort();
console.log(names);
// Bryan,Clayton,Cynthia,David,Mike,Raymond
但是如果数组元素是数字类型,sort()方法的排序结果就不能让人满意了:
var nums = [3,1,2,100,4,200];
nums.sort();
console.log(nums);
//1,100,2,200,3,4
传入一个大小比较函数,再来看看前面的例子:
function compare(num1, num2) {
return num1 - num2;
}
var nums = [3,1,2,100,4,200];
nums.sort(compare);
console.log(nums);
// 1,2,3,4,100,200