Sort IP addresses correctly.

This commit is contained in:
Mitchell Livingston 2006-08-14 23:39:51 +00:00
parent 1043035ce4
commit cc7b033984
3 changed files with 29 additions and 0 deletions

View File

@ -39,4 +39,6 @@
- (NSAttributedString *) attributedStringFittingInWidth: (float) width
attributes: (NSDictionary *) attributes;
- (NSComparisonResult) compareIP: (NSString *) string;
@end

View File

@ -157,4 +157,31 @@
attributes: attributes] autorelease];
}
- (NSComparisonResult) compareIP: (NSString *) string
{
if ([self isEqualToString: string])
return NSOrderedSame;
NSEnumerator * selfSections = [[self componentsSeparatedByString: @"."] objectEnumerator],
* newSections = [[string componentsSeparatedByString: @"."] objectEnumerator];
NSComparisonResult result;
NSString * selfString = [selfSections nextObject], * newString = [newSections nextObject];
while (selfString && newString)
{
if ((result = [selfString compare: newString options: NSNumericSearch]) != NSOrderedSame)
return result;
selfString = [selfSections nextObject];
newString = [newSections nextObject];
}
if (selfString)
return NSOrderedDescending;
else if (newString)
return NSOrderedAscending;
else
return NSOrderedSame;
}
@end