js对数组Array进行排序
2013-05-03 10:20
357 查看
js的数组中有一个sort()办法,默认是遵守ASCII字符次序进行升序分列。sort(sortFun)办法可以接管一个办法为参数,这个办法有两个参数:分别代表每次排序对比时的两个数组项。sort()排序时每次对比两个数组项都应履行这个参数,并把两个对比的数组项作为参数传递给这个函数。当函数返回值为1的时就互换两个数组项的次序,不然就不互换。
代码:
结论:
1.数组调用sort办法后,会影响本身(而非生成新数组)
2.sort()办法默认是按字符来排序的,所以在对数字型数组排序时,不能想当然的认为会按数字大小排序!
3.要改变默认的sort行动(即按字符排序),可以自行指定排序规矩函数(如本例所示)
下面是一个自定义排序的例子,按照二维数组的各类体式格式排序:
var arr= new Array();
arr[0]= new Array(""e"", 2, ""dfg"");
arr[1]= new Array(""b"", 3, ""bcfgf"");
arr[2]= new Array(""a"", 1, ""za"");
arr[3]= new Array(""d"", 4, ""ehfg"");
arr.sort(sortFun);
alert(arr[0] + " --- " + arr[1] + " --- " + arr[2] + " --- " + arr[3])
function sortFun(x,y){ //按照二维数组的第三列的第一个字母的ASCII码来升序排序
return x[2].charCodeAt(0) - y[2].charCodeAt(0);
}
function sortFun(x,y){ //按照二维数组的第二列的数字大小来升序排序
return x[1] - y[1];
}
以上转载自:http://www.cesclub.com/bw/jishuzhongxin/Webjishu/2012/0303/23673.html
再举一个我在实际项目中使用的例子(Ajax实现):
后台代码:
innerHtml函数:
代码:
var arr = [1,11,23,51,56,100,3,4,1,5,0,5,7,562,0,67,10,50]; arr.sort(); //调用sort办法后,数组本身会被改变,即影响原数组 alert(arr.join(","));//0,0,1,1,10,100,11,23,3,4,5,5,50,51,56,562,67,7 不遵守数字大小排序 arr.sort(function(a,b){return a>b?1:-1});//从小到大排序 alert(arr.join(","));//0,0,1,1,3,4,5,5,7,10,11,23,50,51,56,67,100,562 arr.sort(function(a,b){return a<b?1:-1});//从大到小排序 alert(arr.join(","));//562,100,67,56,51,50,23,11,10,7,5,5,4,3,1,1,0,0
结论:
1.数组调用sort办法后,会影响本身(而非生成新数组)
2.sort()办法默认是按字符来排序的,所以在对数字型数组排序时,不能想当然的认为会按数字大小排序!
3.要改变默认的sort行动(即按字符排序),可以自行指定排序规矩函数(如本例所示)
下面是一个自定义排序的例子,按照二维数组的各类体式格式排序:
var arr= new Array();
arr[0]= new Array(""e"", 2, ""dfg"");
arr[1]= new Array(""b"", 3, ""bcfgf"");
arr[2]= new Array(""a"", 1, ""za"");
arr[3]= new Array(""d"", 4, ""ehfg"");
arr.sort(sortFun);
alert(arr[0] + " --- " + arr[1] + " --- " + arr[2] + " --- " + arr[3])
function sortFun(x,y){ //按照二维数组的第三列的第一个字母的ASCII码来升序排序
return x[2].charCodeAt(0) - y[2].charCodeAt(0);
}
function sortFun(x,y){ //按照二维数组的第二列的数字大小来升序排序
return x[1] - y[1];
}
以上转载自:http://www.cesclub.com/bw/jishuzhongxin/Webjishu/2012/0303/23673.html
再举一个我在实际项目中使用的例子(Ajax实现):
后台代码:
/** * 通过员工姓名查询(员工姓名可以有重名) * @param staff_name:员工姓名 */ public void selectStaffByName(String staff_name){ List<HashMap<String, Object>> hms = new ArrayList<HashMap<String,Object>>(); List<Staff> staffs = staffImp.selListByStaffName(staff_name); for(int i=0;i<staffs.size();i++){ HashMap<String, Object> hm = new HashMap<String, Object>(); Staff staff = staffs.get(i); Dept dept = deptImp.selDeptByDeptId(staff.getDept_id()); DTwo dtwo = deptImp.selDtwoByDtwoId(staff.getDtwo_id()); DThree dthree = deptImp.selDthreeByDthreeId(staff.getDthree_id()); Job job = jobImp.selJobById(staff.getJob_id()); Level level = levelImp.selLevelById(staff.getLevel_id()); hm.put("staff", staff); hm.put("dept", dept); hm.put("dtwo", dtwo); hm.put("dthree", dthree); hm.put("job", job); hm.put("level", level); hms.add(hm); } String hmsStr = JsonUtil.list2json(hms); System.out.println("按姓名查询结果是:"+hmsStr); out.print(hmsStr); }前台js代码(假设我想按员工号进行排序显示):
var isNumberClickOdd = true;//用于记录类似当第一次点击时正向排序,再点一次反向排序 function selectAllOrderByNumber(){ curPage = 1; if(isNumberClickOdd){ lists.sort(function order(x,y){ return x.staff.staff_number - y.staff.staff_number;//主要代码! }); isNumberClickOdd = false; }else if(!isNumberClickOdd){ lists.sort(function order(x,y){ return y.staff.staff_number - x.staff.staff_number;//主要代码! }); isNumberClickOdd = true; } fenye();//调用分页显示函数 }分页函数代码:
var pageSize = 14;//每页大小 var curPage = 1;//当前页,默认第一页 var pageTotal;//总页数 function fenye(){ //设置总页数 document.getElementById("pageTotal").innerHTML = pageTotal; //更新当前页数 document.getElementById("pageCur").innerHTML = curPage; //alert("当前页是:"+curPage); var pageList = new Array(); //每一页的list var min = (pageSize*curPage)-pageSize; var max = pageSize*curPage; if(curPage == pageTotal){ max = lists.length; } for(var i=min; i<max; i++){ var staffView = lists[i]; pageList[i-min] = staffView; } innerHtml(pageList);//调用显示成表格样函数 }
innerHtml函数:
/** * 用于显示 * @param staffViewList 需要显示的列表staffs * @return */ function innerHtml(staffViewList){ var result = " <table style='background-color:#def2f9; width:1230px' cellspacing='1'>" + " <tr> " + "<td>" + "<table id='table_result' >"; result += "<tr >" + "<th style='width:40px' class='td' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>序号</th>" + "<th style='width:70px' class='td' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>姓名</th>" + "<th style='width:90px' class='td' onclick='selectAllOrderByNumber();' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>员工号</th>" + "<th style='width:90px' class='td' onclick='selectAllOrderByDept();' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>部门</th>" + "<th style='width:80px' class='td' onclick='selectAllOrderByJob();' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>职位</th>" + "<th style='width:80px' class='td' onclick='selectAllOrderByLevel();' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>技能等级</th>" + "<th style='width:100px' class='td' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>联系电话</th>" + "<th style='width:80px' class='td' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>籍贯</th>" + "<th style='width:120px' class='td' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>现住址</th>" + "<th style='width:80px' class='td' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>在职状态</th>" + "<th style='width:90px' class='td' onclick='selectAllOrderByStartdate();' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>入职时间</th>" + "<th style='width:90px' class='td' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>离职时间</th>" + "<th style='width:150px' class='td' onmouseover='changeColorLine(this);' onmouseout='restoreColor(this);'>身份证号</th>" + "</tr>"; for(var i=0;i<staffViewList.length;i++) { result += "<tr id="+staffViewList[i].staff.staff_id+" ondblclick='newPage(this.id)'"+" onmouseover='changeColor(this);' >"; result += "<td class='td'>"+(i+1)+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_name+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_number+" "+"</td>"; result += "<td class='td'>"+ showLowDeptName(staffViewList[i].dept.dept_name,staffViewList[i].dtwo.dtwo_name,staffViewList[i].dthree.dthree_name) +" "+"</td>"; result += "<td class='td'>"+staffViewList[i].job.job_name+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].level.level_name+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_phone+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_nativeplace+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_address+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_state+" "+"</td>"; result += "<td class='td'>"+timeStamp2String(staffViewList[i].staff.staff_startdate.time)+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_enddate+" "+"</td>"; result += "<td class='td'>"+staffViewList[i].staff.staff_idcard+" "+"</td>"; result += "</tr>"; } result += "</table></td> </tr></table>"; document.getElementById("div_select_result").innerHTML = result; }
相关文章推荐
- array_multisort — 对多个数组或多维数组进行排序
- js sort 数组排序 Array Sort
- [ActionScript 3.0] 对数组中的元素进行排序Array.sort()的方法
- js:数组重排序问题:如何使用sort()方法按数值的大小进行升序或降序排列
- js sort方法根据数组中对象的某一个属性值进行排序
- array_multisort()函数对多个数组或多维数组进行排序
- js如何对数组进行排序
- PHP使用array_multisort对多个数组或多维数组进行排序
- js使用Array.prototype.sort()对数组对象排序的方法
- js如何数组进行排序?
- js sort方法根据数组中对象的某一个属性值进行排序
- JS对数组进行排序 sort()
- js中数组(Array)的排序(sort)注意事项
- .net 利用array.sort对结构数组进行排序
- array_multisort 对关联数组进行排序的问题 PHP
- 编写一个使用数组类模板Array对数组进行排序、求最大值和求元素和的程序,并采用相关数据进行测试。
- JS数组去重,js中数组(Array)的排序(sort)
- JS 对象数组Array 根据对象object key的值排序sort,很风骚哦
- js中数组(Array)的排序(sort)注意事项
- js中数组(Array)的排序(sort)注意事项说明