1 var arr1 = [{ 2 id: 8, 3 name: 'a' 4 }, { 5 id: 10, 6 name: 'b' 7 }, { 8 id: 12, 9 name: 'c' 10 }, { 11 id: 6, 12 name: 'd' 13 }, { 14 id: 10, 15 name: 'm' 16 }]; 17 function bubbleSort(array) { 18 /*给每个未确定的位置做循环*/ 19 for (var unfix = array.length - 1; unfix > 0; unfix--) { 20 /*给进度做个记录,比到未确定位置*/ 21 for (var i = 0; i < unfix; i++) { 22 if (array[i].id > array[i + 1].id) { 23 var temp = array[i]; 24 array.splice(i, 1, array[i + 1]); 25 array.splice(i + 1, 1, temp); 26 } 27 } 28 } 29 return array; 30 } 31 console.log(bubbleSort(arr1));