标签:class 数字 abc orange nts ever app 字母 poi
sort()
sort() 方法以字母顺序对数组进行排序:
var arr= ["Banana", "Orange", "Apple", "Mango"]; arr.sort(); // arr = [ "Apple","Banana","Mango","Orange"] var arr= ["1", "4", "2", "3"]; arr.sort(); // arr= ["1", "2", "3", "4"];
数字排序(数字和升序):
var points = [40,100,1,5,25,10]; points.sort(function(a,b){return a-b}); // 1,5,10,25,40,100
数字排序(数字和降序):
var points = [40,100,1,5,25,10]; points.sort(function(a,b){return b-a}); // 100,40,25,10,5,1
根据对象的某个属性来排序
var points = [{a: 1, b: ‘abc‘}, {a: 2, b: ‘cde‘}, {a: 4, b: ‘bba‘}, {a: 3, b: ‘def‘}]; points.sort(function(a,b){ return parseInt(a.a) - parseInt(b.a) }); // [{a: 1, b: ‘abc‘}, {a: 2, b: ‘cde‘}, {a: 3, b: ‘def‘},{a: 4, b: ‘bba‘},];
reverse()
reverse() 方法反转数组中的元素。
var arr= ["1", "4", "2", "3"]; arr.sort(); // arr= ["1", "2", "3", "4"]; // 可以先用 sort() 进行水需排序 reverse() 逆反元素, 可以达到倒序的效果 arr.reverse(); // arr= ["4", "3", "2", "1"];
标签:class 数字 abc orange nts ever app 字母 poi
原文地址:https://www.cnblogs.com/lvsk/p/12001309.html