<!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) { return b - a; }var myarr = new Array("80","16","50","6","100","1");document.write(myarr.sort(sortNum));</script></head><body></body></html>
1 回答
已采纳
_潇潇暮雨
TA贡献646条经验 获得超225个赞
sort(fn),如果不传fn则按照字典序进行排列,传入fn则根据指定规则进行排序,myarr的排序结果是:[ '1', '100', '16', '50', '6', '80' ]
传入的函数表示按照数字(因为减号操作符隐含着将操作数转化为数字)进行降序排列,排序结果是:[ '100', '80', '50', '16', '6', '1' ]
添加回答
举报
0/150
提交
取消