JavaScript对象排序_hanCSDN_20130111

来源:互联网 发布:燕氏姓名学软件 编辑:程序博客网 时间:2024/06/04 00:45
<script type="text/javascript">function t_obj(i,param){this.name = i + "";this.isUsed = param;}var arr = [];obj0 = new t_obj(0,1);obj1 = new t_obj(1,0);obj2 = new t_obj(2,1);obj3 = new t_obj(3,1);obj4 = new t_obj(4,0);obj5 = new t_obj(5,0);obj6 = new t_obj(6,1);obj7 = new t_obj(7,1);obj8 = new t_obj(8,0);obj9 = new t_obj(9,0);arr[0] = obj0;arr[1] = obj1;arr[2] = obj2;arr[3] = obj3;arr[4] = obj4;arr[5] = obj5;arr[6] = obj6;arr[7] = obj7;arr[8] = obj8;arr[9] = obj9;alert("排序前");for(var i=0;i<arr.length;i++){alert("name--->" + arr[i].name + "flag--->" + arr[i].isUsed);}function px(){var temp;var n;var m;for(n=0; n<arr.length; n++) {   for(m=n+1; m<arr.length; m++) {if(arr[n].isUsed < arr[m].isUsed) {       temp = arr[n];                     arr[n] = arr[m];                       arr[m] = temp;}}}temp = null;n = null;m = null;}px();alert("排序后");for(var i=0;i<arr.length;i++){alert("name--->" + arr[i].name + "flag--->" + arr[i].isUsed);}</script>

原创粉丝点击