sephlaire
01-25-2012, 06:41 PM
I am trying to make an addition to a .js plugin for a wiki. Currently it does not support sorting my IP address. So far I have created the below code which does more than the script initially did, but is still not sorting 100% correctly.
Any tips?
sort_ipaddr: function(a,b){
aa = a[0].split(".",4);
bb = b[0].split(".",4);
var counti = 0;
for (var i=0; i<4; i++) {
if (parseInt(parseFloat(aa[i])) == parseInt(parseFloat(bb[i]))){}
if (parseInt(parseFloat(aa[i])) < parseInt(parseFloat(bb[i]))){counti--}
if (parseInt(parseFloat(aa[i])) > parseInt(parseFloat(bb[i]))){counti++}
}
return counti;
},
EDIT
I've also tried this which is closer but still not there.
sort_ipaddr: function(a,b){
aa = a[0].split(".",4);
bb = b[0].split(".",4);
var resulta = (aa[3]+(aa[2]*256)+(aa[1]*256*256)+(aa[0]*256*256*256));
var resultb = (bb[3]+(bb[2]*256)+(bb[1]*256*256)+(bb[0]*256*256*256));
return resulta-resultb;
},
This results in a list like so:
10.1.15.22
10.1.16.22
10.1.15.23
10.1.15.24
10.1.16.24
10.1.15.25
Any tips?
sort_ipaddr: function(a,b){
aa = a[0].split(".",4);
bb = b[0].split(".",4);
var counti = 0;
for (var i=0; i<4; i++) {
if (parseInt(parseFloat(aa[i])) == parseInt(parseFloat(bb[i]))){}
if (parseInt(parseFloat(aa[i])) < parseInt(parseFloat(bb[i]))){counti--}
if (parseInt(parseFloat(aa[i])) > parseInt(parseFloat(bb[i]))){counti++}
}
return counti;
},
EDIT
I've also tried this which is closer but still not there.
sort_ipaddr: function(a,b){
aa = a[0].split(".",4);
bb = b[0].split(".",4);
var resulta = (aa[3]+(aa[2]*256)+(aa[1]*256*256)+(aa[0]*256*256*256));
var resultb = (bb[3]+(bb[2]*256)+(bb[1]*256*256)+(bb[0]*256*256*256));
return resulta-resultb;
},
This results in a list like so:
10.1.15.22
10.1.16.22
10.1.15.23
10.1.15.24
10.1.16.24
10.1.15.25