<!Doctype html> <html xmlns="http://www./1999/xhtml"> <head> <title>数组方法sort</title>
<style type="text/css"> #show{ width:200px; height:200px; margin:0 auto; color:white; background:#2c2c2c; text-align:center }
</style>
<script type="text/javascript"> var str="" var a = [33,4,111,22] // 方法用于对数组的元素进行排序。 a.sort(function(a, b) { return a - b; }) //function(a, b) { return a - b; 排序的依据} str=a; document.write(str)
var str = "" var a=['dog','ant','catch','bug']
a.sort(function(x, y) { var x = x.toLowerCase(); // 转成小写 //转大写 toUppperCase() var y = y.toLowerCase(); if (x < y) return -1; if (x > y) return 1; return 0; }) str=a
document.write("<div id='show'>"+str+"</div>")
</script>
</head>
<body> </body>
</html>
|