Changeset 777


Ignore:
Timestamp:
Aug 14, 2006, 11:39:51 PM (15 years ago)
Author:
livings124
Message:

Sort IP addresses correctly.

Location:
trunk/macosx
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/StringAdditions.h

    r357 r777  
    4040                                attributes: (NSDictionary *) attributes;
    4141
     42- (NSComparisonResult) compareIP: (NSString *) string;
     43
    4244@end
  • trunk/macosx/StringAdditions.m

    r424 r777  
    158158}
    159159
     160- (NSComparisonResult) compareIP: (NSString *) string
     161{
     162    if ([self isEqualToString: string])
     163        return NSOrderedSame;
     164
     165    NSEnumerator * selfSections = [[self componentsSeparatedByString: @"."] objectEnumerator],
     166            * newSections = [[string componentsSeparatedByString: @"."] objectEnumerator];
     167   
     168    NSComparisonResult result;
     169    NSString * selfString = [selfSections nextObject], * newString = [newSections nextObject];
     170    while (selfString && newString)
     171    {
     172        if ((result = [selfString compare: newString options: NSNumericSearch]) != NSOrderedSame)
     173            return result;
     174       
     175        selfString = [selfSections nextObject];
     176        newString = [newSections nextObject];
     177    }
     178   
     179    if (selfString)
     180        return NSOrderedDescending;
     181    else if (newString)
     182        return NSOrderedAscending;
     183    else
     184        return NSOrderedSame;
     185}
     186
    160187@end
Note: See TracChangeset for help on using the changeset viewer.