title: Sort IP address
body: <p>I'm working on an application which scan the wi-fi network and list the ip list and device connected.</p>
  
  <p>I tried to sort the ip list by creating two new object properties ip4thOctet and ip3thOctet and make the sort after depend of them.</p>
  
  <pre><code>@property (nonatomic, assign) NSInteger ip4thOctet;
  @property (nonatomic, assign) NSInteger ip3thOctet;
  </code></pre>
  
  <p>and: </p>
  
  <pre><code>-(NSInteger)ip4thOctet {
      if (self.ipAddress) {
              return [[[self.ipAddress componentsSeparatedByString:@"."] lastObject] intValue];
      }
      return 0;
  }
  
  -(NSInteger)ip3thOctet {
      if (self.ipAddress) {
          NSInteger elements = [[self.ipAddress componentsSeparatedByString:@"."] count];
          return [[[self.ipAddress componentsSeparatedByString:@"."] objectAtIndex:elements-1] intValue];
      }
      return 0;
  }
  </code></pre>
  
  <p>Based on these properties I tried to sort the array:</p>
  
  <pre><code>-(void)lanScanDidFindNewDevice:(Device*)device {
      if (![connectedDevicesMutable containsObject:device]) {
          [connectedDevicesMutable addObject:device];
      }
  
      self.connectedDevices = [NSMutableArray arrayWithArray:connectedDevicesMutable];
  
      NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ip4thOctet"
                                                   ascending:YES
                                                    selector:@selector(compare:)
                        ];
      NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
      self.connectedDevices = [self.connectedDevices sortedArrayUsingDescriptors:sortDescriptors];
  
  
      NSSortDescriptor *sortDescriptor3thIpOctect = [[NSSortDescriptor alloc] initWithKey:@"ip3thOctet"
                                                   ascending:YES
                                                    selector:@selector(compare:)
                        ];
      NSArray *sortDescriptors3th = [NSArray arrayWithObject:sortDescriptor3thIpOctect];
      self.connectedDevices = [self.connectedDevices sortedArrayUsingDescriptors:sortDescriptors3th];
  }
  </code></pre>
  
  <p>The client expected result should looks like :</p>
  
  <p>Expected:</p>
  
  <p>172.17.0.1</p>
  
  <p>172.17.0.2</p>
  
  <p>172.17.1.1</p>
  
  <p>172.17.1.3</p>
  
  <p>172.17.3.1</p>
  
  <p>Can you help me to sort the ip list according with client expectation, please ?</p>
  
  <p>The current result is :</p>
  
  <p>172.17.0.1</p>
  
  <p>172.17.1.1</p>
  
  <p>172.17.3.1</p>
  
  <p>172.17.0.2</p>
  
  <p>172.17.1.2</p>
  
  <p>172.17.0.3</p>
  
  <p>172.17.1.3</p>