js给某个数组按照ip大小排序
const hips = [
{ip: '143.124.2.3'},
{ip: '10.124.2.8'},
{ip: '12.164.6.3'},
{ip: '10.124.0.99'},
{ip: '10.14.0.43'},
{ip: '14.124.0.0'},
{ip: '10.154.0.5'},
]
function sortby(ips) {
ips.sort((a, b) => {
let arr1 = a.ip.split('.')
let arr2 = b.ip.split('.')
for (let i = 0; i < 4; i++) {
if(arr1[i] > arr2[i]) {
return 1
} else if(arr1[i] < arr2[i]){
return -1
}
}
})
return ips
}
console.log(sortby(hips))
实现效果: