博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript按IP地址排序
阅读量:5824 次
发布时间:2019-06-18

本文共 6531 字,大约阅读时间需要 21 分钟。

  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

 

 

  反序:

  

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 }
View Code

 

  这是其他排序的方法,欢迎查看,欢迎给意见!

  

  

  

转载于:https://www.cnblogs.com/nelsonlei/p/8440684.html

你可能感兴趣的文章
tomcat 实例
查看>>
MyBatis使用DEMO及cache的使用心得
查看>>
网站文章如何能自动判定是抄袭?一种算法和实践架构剖析
查看>>
【OpenCV学习】滚动条
查看>>
ofo用科技引领行业进入4.0时代 用户粘性连续8个月远甩摩拜
查看>>
无法拒绝|华为618最高优惠1000元 更有梅西签名球衣奉送
查看>>
乐信Q2季报图解:调整后净利过5亿 同比增长776%
查看>>
兰州青年志愿者“中西合璧”玩快闪 温暖旅客回家路
查看>>
计划10年建10万廉价屋 新西兰政府:比想象中难
查看>>
甘肃发首版《3D打印职业教育教材》:校企合作育专才
查看>>
内蒙古2019年精准脱贫新“目标”:20个贫困旗县全部摘帽
查看>>
韩国国会议员涉嫌投机炒房 检方称已立案调查
查看>>
李娜入选国际网球名人堂 成亚洲第一人
查看>>
为找好心人抚养孩子 浙江一离婚父亲将幼童丢弃公园
查看>>
晚婚晚育 近20年巴西35岁以上孕妇增加65%
查看>>
读书:为了那个美妙的咔哒声
查看>>
我从过去八个月的AI公司面试中学到了什么?
查看>>
jQuery实践小结
查看>>
深入探究Immutable.js的实现机制(一)
查看>>
jsp改造之sitemesh注意事项
查看>>