Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What would i need to put in the SortIP function to make the custom sort function sort the array by the last digit of the IP number. This doesn't work.

function SortIP(a, b)
{
    return a[0][3] - b[0][3];
}

LAN = new Array
(
    ["ADSL Router", [192, 168, 0, 1]],
    ["Gary's Mac", [192, 168, 0, 15]],
    ["Network Switch", [192, 168, 0, 2]],
    ["Production Email", [192, 168, 0, 60]]
);
LAN.sort(SortIP);

expected array order:

  1. ADSL Router: 192.168.0.1
  2. Network Switch: 192.168.0.2
  3. Gary's Mac: 192.168.0.15
  4. Production Email: 192.168.0.60
share|improve this question
 
You should use the array literal syntax [ … ] and not new Array(). –  Gumbo Jun 25 '09 at 18:56
 
why ? –  Gary Willoughby Jun 26 '09 at 18:49
add comment

5 Answers

up vote 5 down vote accepted

You're almost there

just replace

return a[0][3] - b[0][3];

with

return a[1][3] - b[1][3];

and you're done.

why? cuz the IP is the second (Index=1) cell of each array.

share|improve this answer
 
Talk about 'can't see the wood for the trees'! Thanks. –  Gary Willoughby Jun 25 '09 at 18:54
 
You're welcome :) Happens to me each and every day at work –  Ken Egozi Jun 25 '09 at 19:47
add comment

You’re comparing the wrong values. Try this:

function SortIP(a, b) {
    return a[1][3] - b[1][3];
}
share|improve this answer
add comment

The values sent to the sort handler are values of the array being sorted.

Since this is a bubble sort, you have to return 0 if the items are the same, 1 if a > b, and -1 if b > a;

function SortIP(a, b)
{
    if ( a[1][3] == b[1][3] ) return 0;
    return ( a[1][3] > b[1][3] ) ? 1 : -1;
}
share|improve this answer
 
@Peter: return a[1][3] - b[1][3] will return 0 if they are the same. There is a potential for overflow when using 'a - b', but since he is sorting the last value of an IP address, we know that the value will never result in an overflow. –  Grant Wagner Jun 25 '09 at 18:56
 
Yeah, I sorta figured it out after I posted. I'll leave this as it is, though, as an explicit method for doing the sort. –  Peter Bailey Jun 25 '09 at 19:05
add comment

change your sort function to:

function SortIP(a, b)
{
    return a[1][3] - b[1][3];
}
share|improve this answer
add comment

If you want to sort by the complete address, it might be a good idea to write a thin wrapper for the array containing the bytes:

function IP4() {
    var ip = Array.prototype.slice.call(arguments, 0, 4);
    ip.toString = IP4.toString;
    ip.valueOf = IP4.valueOf;
    return ip;
}

IP4.toString = function() {
    return this.join('.');
};

IP4.valueOf = function() {
    return (this[0] << 24) | (this[1] << 16) | (this[2] << 8) | this[3];
};

var LAN = [
    ["ADSL Router", IP4(192, 168, 0, 1)],
    ["Gary's Mac", IP4(192, 168, 0, 15)],
    ["Network Switch", IP4(192, 168, 0, 2)],
    ["Production Email", IP4(192, 168, 0, 60)]
];

LAN.sort(function(a, b) { return a[1] - b[1]; });
share|improve this answer
 
clever idea . –  Gary Willoughby Jun 26 '09 at 18:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.