For reasons of things, you are trying to sort / arrange your IP addresses by network ID, then subnet mask, then node id. I think you misunderstood the role of the subnet mask in the IP address (which is a very common mistake, I made this mistake once too) :).
The subnet mask is used to separate the network identifier from the IP address. A subnet mask is a binary number consisting of a continuous series of 1 bit that starts with the most significant bit of a binary word (4 byte binary value). For instance:
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 (we are going to do a little math here), we get a 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, let's say:
11110000.00000000.00000000.00000000
Then the network identifier will be a logical AND subnet mask and 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 identifier will be logical AND IP addresses and / negation / subnet masks:
11110000.00000000.00000000.00000000
The purpose of all this is to create a contradiction with your understanding of the nature of the subnet mask and, therefore, your sort order. Consider the subnet mask as follows:
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 we distinguish what we created this time compared to the previous time, we calculated the network identifier. The answer should correspond to the subnet mask. The subnet mask does determine the network identifier. The IP address and subnet mask should be considered together, since you need to determine the network ID (which is similar to what you are trying to do).
If you want to organize your data for meaningful manipulations, first translate the source IP address + subnet mask into the correct network identifier and node identification parts. The rest will quickly fall into place.
I really hope this helps a person!