慕移动1741139
2017-08-15 09:55
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array对象 </title>
<script type="text/javascript">
function sortNum(a,b) {
var myarr = new Array("80","16","50","6","100","1");
return b - a;
var soult = myarr.sort(sortNum);
}
document.getElementById("demo").innerHTML=soult;
</script>
</head>
<body>
<button onclick="sortNum(a,b)">点我对函数排序</button>
<p id="demo">80,16,50,6,100,1</p>
</body>
</html>
function sortNum() {
var soult;
var myarr = new Array("80","16","50","6","100","1");
soult = myarr.sort(function compare(a,b){return a -b;});
document.getElementById("demo").innerHTML=soult;
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array对象 </title>
<script type="text/javascript">
function b(){
var myarr = new Array("80","16","50","6","100","1");
function sortNum(a,b) {
return b - a; //升序
}
var soult = myarr.sort(sortNum);
document.getElementById("demo").innerHTML=soult;
}
</script>
</head>
<body>
<button onclick="b()">点我对函数排序</button>
<p id="demo">80,16,50,6,100,1</p>
</body>
</html>
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题