js数组排序

在写js代码时有时需要对二维数组按某个列(或者说按对象数组的某个属性)进行排序,js数组提供了类似于java的sort方法:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> js array sort </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
	var arr=[["aa",1,"cc"],["ad",2,"bd"],["wd",3,"zc"],["wda",4,"df"]];
	//降序
	function desc(a,b){
		//alert(a+"<-->"+b);
		if(a[1]>b[1]){
			return -1;
		}else if(a[1]==b[1]){
			return 0;
		}else{
			return 1;
		}
	}
	arr.sort(desc);
	alert(arr);
  </script>
 </HEAD>

 <BODY>
  
 </BODY>
</HTML>

 

你可能感兴趣的:(js,排序,sort)