您的位置:首页 > Web前端 > JavaScript

js对数组Array进行排序

2013-05-03 10:20 357 查看
js的数组中有一个sort()办法,默认是遵守ASCII字符次序进行升序分列。sort(sortFun)办法可以接管一个办法为参数,这个办法有两个参数:分别代表每次排序对比时的两个数组项。sort()排序时每次对比两个数组项都应履行这个参数,并把两个对比的数组项作为参数传递给这个函数。当函数返回值为1的时就互换两个数组项的次序,不然就不互换。

代码:

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: