码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 学习笔记(权威指南)

时间:2015-07-11 11:56:55      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

1、数组的sort()方法默认是按照字母排序的,下面举个栗子说明:
  1)全是字母:

var arr =["zu","fan","an","peng","zhang"];
arr.sort();
alert(arr);//结果是an,fan,peng,zhang,zu

  2)全是数字:

var arr =["34","12","67","44","668"];
arr.sort();
alert(arr);//结果是12,34,44,668,67

  3)字母数字组合:

var arr =["34","a2","5","y7","6k"];
arr.sort();
alert(arr);//结果是34,5,6k,a2,y7(显示按照数字排序,然后在字母排)

  4)sort()方法可以对数组进行升(降)序排序:

var arr =["34","12","67","44","668"]; 
arr.sort(
function(a,b){
  
return a-b; (若换成b-a,则是降序排列)
});
alert(arr);//12,34,44,67,668

 

javascript 学习笔记(权威指南)

标签:

原文地址:http://www.cnblogs.com/imsomnus/p/4638214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!