var listDoctorTime= [ { "id": 1, "name": "张三", "index": "100" },
{ "id": 2, "name": "李四", "index": 120 },
{ "id": 3, "name": "王武", "index": 80 },
{ "id": 4, "name": "二狗", "index": 40 },
{ "id": 5, "name": "屎蛋", "index": 160 }
];
方法1:冒泡排序
bubbleSortlistDoctorTime(listDoctorTime);
方法2: sort方法
listDoctorTime.sort(function(a,b){
return a.index-b.index;
});
//遍历
for(var w=0;w<listDoctorTime.length;w++){
console.log("listDoctorTime[w].index=="+listDoctorTime[w].index);
}
输出结果:listDoctorTime[w].index==40
listDoctorTime[w].index==80
listDoctorTime[w].index==120
listDoctorTime[w].index==160
//冒泡排序
function bubbleSortlistDoctorTime(listDoctorTime){
if(listDoctorTime.length>1){
for(var i=0;i<listDoctorTime.length-1;i++){//外层控制循环的次数
for(var j=0; j<listDoctorTime.length-i-1;j++ ){//内层控制比较多少次
if(parseInt(listDoctorTime[j+1].index) < parseInt(listDoctorTime[j].index)){
var doctorTime=listDoctorTime[j+1];
listDoctorTime[j+1]=listDoctorTime[j];
listDoctorTime[j]=doctorTime;
}
}
}
}
return listDoctorTime;
}
//简单集合冒泡排序
var nums = new Array(8,9,1,2,10);
function test(){
//进行几轮比较,确定几个位置
for(var i=0;i<nums.length-1;i++){
for(var j=0;j<nums.length-1-i;j++){
if(nums[j]>nums[j+1]){
var tmp=nums[j+1];
nums[j+1]=nums[j];
nums[j]=tmp;
}
}
}
}