JavaScript按IP地址列表排序,主要思路就是分割每个点号部分,然后ip1和ip2分别对不够三位数的进行补0操作,然后转换为数字类型进行一一比较。
上代码:
正序:
1 var arr=[ 2 {ip:"10.20.39.12"}, 3 {ip:"43.120.39.11"}, 4 {ip:"25.210.39.21"}, 5 {ip:"75.110.39.131"}, 6 {ip:"12.230.139.14"}, 7 {ip:"17.20.39.121"}, 8 {ip:"11.220.139.111"}, 9 {ip:"172.30.39.131"}, 10 {ip:"190.10.139.123"}, 11 {ip:"170.20.4.222"} 12 ] 13 14 console.log(IpSortForward(arr,"ip")); 15 16 //正序 17 function IpSortForward(data, p) { 18 for (i = 0; i < data.length - 1; i++) { 19 for (j = 0; j < data.length - 1 - i; j++) { 20 var arr1 = data[j][p].split("."), arr2 = data[j + 1][p].split("."); 21 if (arr1[0].length == 2) { 22 var t = arr1[0]+"0"; 23 arr1[0] = t; 24 }else if(arr1[0].length == 1){ 25 var t = arr1[0]+"00"; 26 arr1[0] = t; 27 }else{} 28 29 30 if (arr1[1].length == 2) { 31 var t = arr1[1]+"0"; 32 arr1[1] = t; 33 }else if(arr1[1].length == 1){ 34 var t = arr1[1]+"00"; 35 arr1[1] = t; 36 }else{} 37 38 39 if (arr1[2].length == 2) { 40 var t = arr1[2]+"0"; 41 arr1[2] = t; 42 }else if(arr1[2].length == 1){ 43 var t = arr1[2]+"00"; 44 arr1[2] = t; 45 }else{} 46 47 48 if (arr1[3].length == 2) { 49 var t = arr1[3]+"0"; 50 arr1[3] = t; 51 }else if(arr1[3].length == 1){ 52 var t = arr1[3]+"00"; 53 arr1[3] = t; 54 }else{} 55 56 if (arr2[0].length == 2) { 57 var t = arr2[0]+"0"; 58 arr2[0] = t; 59 }else if(arr1[0].length == 1){ 60 var t = arr2[0]+"00"; 61 arr2[0] = t; 62 }else{} 63 64 65 if (arr2[1].length == 2) { 66 var t = arr2[1]+"0"; 67 arr2[1] = t; 68 }else if(arr2[1].length == 1){ 69 var t = arr2[1]+"00"; 70 arr2[1] = t; 71 }else{} 72 73 74 if (arr2[2].length == 2) { 75 var t = arr2[2]+"0"; 76 arr2[2] = t; 77 }else if(arr2[2].length == 1){ 78 var t = arr2[2]+"00"; 79 arr2[2] = t; 80 }else{} 81 82 83 if (arr2[3].length == 2) { 84 var t = arr2[3]+"0"; 85 arr2[3] = t; 86 }else if(arr2[3].length == 1){ 87 var t = arr2[3]+"00"; 88 arr2[3] = t; 89 }else{} 90 91 92 var param1=arr1[0]+arr1[1]+arr1[2]+arr1[3]; 93 var param2=arr2[0]+arr2[1]+arr2[2]+arr2[3]; 94 95 if (parseInt(param1) > parseFloat(param2)) { 96 var temp = data[j]; 97 data[j] = data[j + 1]; 98 data[j + 1] = temp; 99 }100 }101 }102 return data;103 }
反序:
1 var arr=[ 2 {ip:"10.20.39.12"}, 3 {ip:"43.120.39.11"}, 4 {ip:"25.210.39.21"}, 5 {ip:"75.110.39.131"}, 6 {ip:"12.230.139.14"}, 7 {ip:"17.20.39.121"}, 8 {ip:"11.220.139.111"}, 9 {ip:"172.30.39.131"}, 10 {ip:"190.10.139.123"}, 11 {ip:"170.20.4.222"} 12 ] 13 14 console.log(IpSortForward(arr,"ip")); 15 16 17 //反序 18 function IpSortReverse(data, p) { 19 for (i = 0; i < data.length - 1; i++) { 20 for (j = 0; j < data.length - 1 - i; j++) { 21 var arr1 = data[j][p].split("."), arr2 = data[j + 1][p].split("."); 22 if (arr1[0].length == 2) { 23 var t = arr1[0]+"0"; 24 arr1[0] = t; 25 }else if(arr1[0].length == 1){ 26 var t = arr1[0]+"00"; 27 arr1[0] = t; 28 }else{} 29 30 31 if (arr1[1].length == 2) { 32 var t = arr1[1]+"0"; 33 arr1[1] = t; 34 }else if(arr1[1].length == 1){ 35 var t = arr1[1]+"00"; 36 arr1[1] = t; 37 }else{} 38 39 40 if (arr1[2].length == 2) { 41 var t = arr1[2]+"0"; 42 arr1[2] = t; 43 }else if(arr1[2].length == 1){ 44 var t = arr1[2]+"00"; 45 arr1[2] = t; 46 }else{} 47 48 49 if (arr1[3].length == 2) { 50 var t = arr1[3]+"0"; 51 arr1[3] = t; 52 }else if(arr1[3].length == 1){ 53 var t = arr1[3]+"00"; 54 arr1[3] = t; 55 }else{} 56 57 if (arr2[0].length == 2) { 58 var t = arr2[0]+"0"; 59 arr2[0] = t; 60 }else if(arr1[0].length == 1){ 61 var t = arr2[0]+"00"; 62 arr2[0] = t; 63 }else{} 64 65 66 if (arr2[1].length == 2) { 67 var t = arr2[1]+"0"; 68 arr2[1] = t; 69 }else if(arr2[1].length == 1){ 70 var t = arr2[1]+"00"; 71 arr2[1] = t; 72 }else{} 73 74 75 if (arr2[2].length == 2) { 76 var t = arr2[2]+"0"; 77 arr2[2] = t; 78 }else if(arr2[2].length == 1){ 79 var t = arr2[2]+"00"; 80 arr2[2] = t; 81 }else{} 82 83 84 if (arr2[3].length == 2) { 85 var t = arr2[3]+"0"; 86 arr2[3] = t; 87 }else if(arr2[3].length == 1){ 88 var t = arr2[3]+"00"; 89 arr2[3] = t; 90 }else{} 91 92 93 var param1=arr1[0]+arr1[1]+arr1[2]+arr1[3]; 94 var param2=arr2[0]+arr2[1]+arr2[2]+arr2[3]; 95 96 if (parseInt(param1) < parseFloat(param2)) { 97 var temp = data[j]; 98 data[j] = data[j + 1]; 99 data[j + 1] = temp;100 }101 }102 }103 return data;104 }
这是其他排序的方法,欢迎查看,欢迎给意见!