By the looks of things you are trying to sort/organize your IP addresses by network id, then subnet mask, then node id. I think you misunderstood the role of a subnet mask in IP addressing (which is a very common mistake man, I made this mistake too once upon a time :) ).
A subnet mask is used to separate a network id from an IP address. A subnet mask is a binary number that consists of an uninterrupted series of 1 bits which begins from the most significant bit of a binary word(4 byte binary value). For example:
11110000.00000000.00000000.00000000 // subnet_mask: /4
11111000.00000000.00000000.00000000 // subnet_mask: /5
11011000.00000000.00000000.00000000 // not a legal bitmask because of the 0 bit
When you consider a random IP address such as 74.125.224.197 (gonna do a little math here) we get the bit pattern:
01001010.01111101.11100000.11000101 // This is the same as 74.125.224.197
Now if we have a subnet mask for this particular IP of say:
11110000.00000000.00000000.00000000
Then the network id is going to be the logical AND of the subnet mask and the IP address:
01001010.01111101.11100000.11000101 // ip
& 11110000.00000000.00000000.00000000 // subnet mask
---------------------------------------
01000000.00000000.00000000.00000000 // Network Id
Along logically similar lines, the node id is going to be the logical AND of the IP address and the /negation/ of the subnet mask:
/* negation of subnet mask via XOR */
11110000.00000000.00000000.00000000 // subnet mask
XOR 11111111.11111111.11111111.11111111
---------------------------------------
00001111.11111111.11111111.11111111 // negation of subnet mask
/* getting the node id from the IP address using the negation of subnet mask */
00001111.11111111.11111111.11111111 // negation of subnet mask
& 01001010.01111101.11100000.11000101 // original IP address
---------------------------------------
00001010.01111101.11100000.11000101 // the node Id
The point of all this is to set up a contradiction to your understanding of the nature of the subnet mask and therefore your sort order. Consider a subnet mask like this:
01001010.01111101.11100000.11000101 // original IP address of google
& 11000000.00000000.00000000.00000000 // different subnet mask
---------------------------------------
01000000.00000000.00000000.00000000 // same network id(sort of) as above
How do we differentiate between what we generated this time versus the previous time we calculated the network id. The answer has to do with the subnet mask. The subnet mask is really about determining the network id. The IP address and the subnet mask should be considered together since you require both to determine a network id(which is what it looks like you are trying to order by).
If you want to organize your data for meaningful manipulation, consider first translating the raw IP address + subnet mask into proper network id and node id parts. The rest will quickly fall into place for you.
I truly hope this helps man!