JavaScript按IP地址排序

  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 }
View Code
43人参与, 0条评论 登录后显示评论回复

你需要登录后才能评论 登录/ 注册