JavaScript 数组sort方法使用

Posted on 2019-08-18 05:29  任旭_2014  阅读(299)  评论(0编辑  收藏  举报

直接上例子

1.无参调用返回按unicode字符编码排序

var arr = [1,2,0,21,15,6,34,9,45];
var arrSort = arr.sort();
console.log(arrSort);//会返回[0,1,15,2,21,34,45,6,9]

2.有参调用,参数为匿名函数

var arr = [1,2,0,21,15,6,34,9,45];
var arrSort = arr.sort(function(a,b){return a-b});
console.log(arrSort);//会返回[0,1,2,6,9,15,21,34,45]

Copyright © 2024 任旭_2014
Powered by .NET 9.0 on Kubernetes