Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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 08:27 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    sephlaire (01-25-2012)

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts
    --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 09:35 PM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    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.
    Last edited by Old Pedant; 01-25-2012 at 10:01 PM.
    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.

  • Users who have thanked Old Pedant for this post:

    sephlaire (01-25-2012)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    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.

  • #7
    New to the CF scene
    Join Date
    Nov 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This thread helped me to come up with this to sort an array of IP addresses:
    myIpArray.sort(function(a,b){ // sort IP address.
    aa = getIP(a).split(".");
    bb = getIP(b).split(".");

    for (var i=0, n=Math.max(aa.length, bb.length); i<n; i++) {
    if (aa[i] !== bb[i]) return aa[i] - bb[i];
    }
    return 0;
    });

  • #8
    New to the CF scene
    Join Date
    Nov 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Better format
    Code:
    myIpArray.sort(function(a,b){ // sort IP address.
        aa = getIP(a).split(".");
        bb = getIP(b).split(".");
      
        for (var i=0, n=Math.max(aa.length, bb.length); i<n; i++) {
            if (aa[i] !== bb[i]) return aa[i] - bb[i];
        }
    
        return 0;
    });

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Yes, that works, but However, I would *bet* that my code, to convert the strings into numbers, would be faster because it would avoid the overhead of the loop.

    In other words:
    Code:
    myIpArray.sort(
        function( a, b )
        {
    	var aa = getIP(a).split(".");
    	var bb = getIP(b).split(".");
    	
            return ( aa[0]*0x1000000 + aa[1]*0x10000 + aa[2]*0x100 + aa[3]*1 )
                 - ( bb[0]*0x1000000 + bb[1]*0x10000 + bb[2]*0x100 + bb[3]*1 );
        }
    );
    However, since you are supplying that getIP() function [I have no idea what it is doing...I assume it is somehow extracting the IP address from the given object variable], why not *also* supply a getIpNumber( ) function that indeed returns the ip address *as* an integer number?

    So then the code could be as simple as
    Code:
    myIpArray.sort( function( a, b ) { return getIpNumber(a) - getIpNumber(b); } );
    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.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    And as a minor point: If you are using a MySQL DB, you can easily ask for a sort by IP address in your SQL code:
    Code:
    SELECT * FROM sometable ORDER BY INET_ATON( ipaddress )
    Probably not relevant to this thread, but something to keep in mind.
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •