Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2012, 06:41 PM   PM User | #1
sephlaire
New Coder

 
Join Date: Jan 2012
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
sephlaire is an unknown quantity at this point
Sorting Ip address within array

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?

Code:
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.
Code:
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

Last edited by sephlaire; 01-25-2012 at 07:27 PM..
sephlaire is offline   Reply With Quote
Old 01-25-2012, 08:19 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Your second one is close, but the very first addition in each resultX calculation will be a string concatenation instead of arithmetic add. So that means that you end up comparing something like "24xxxxx" vs. "25xxxxx" and get the results you are seeing.

Keep it simple:
Code:
sort_ipaddr: function(a,b){
	aa = a[0].split(".");
	bb = b[0].split(".");
	
        var resulta = aa[0]*0x1000000 + aa[1]*0x10000 + aa[2]*0x100 + aa[3]*1;
        var resultb = ab[0]*0x1000000 + ab[1]*0x10000 + ab[2]*0x100 + ab[3]*1;
	
	return resulta-resultb;
  },
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
sephlaire (01-25-2012)
Old 01-25-2012, 08:32 PM   PM User | #3
sephlaire
New Coder

 
Join Date: Jan 2012
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
sephlaire is an unknown quantity at this point
--EDIT Removed--

I spoke too soon! Works perfectly thank you. My mistake was that in the split I used variables aa and bb. In the resultsx line aa and ab were used as variables.

Last edited by sephlaire; 01-25-2012 at 08:35 PM..
sephlaire is offline   Reply With Quote
Old 01-25-2012, 08:57 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oops...I missed that, too. But glad it works now.

If you didn't know, multiplying by anything (including 1) is one way to force the conversion of the string you get from split to become a number.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 01-25-2012 at 09:01 PM..
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
sephlaire (01-25-2012)
Old 01-26-2012, 09:07 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
If you didn't know, multiplying by anything (including 1) is one way to force the conversion of the string you get from split to become a number.
I thought you considered that to be a hack!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 01-26-2012, 08:37 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
LOL! Depends on usage. Here, the whole point was to multiply each part of the IP address by the apprpriate number. I certainly wouldn't do parseInt(ip[0[) * 0x1000000 so why not use the same pattern throughout?

Beside, if it really is an IP address we *know* it will be a number. No detection of invalid values needed, one hopes.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:47 PM.


Advertisement
Log in to turn off these ads.