Re: kCFCompareNumerically
Re: kCFCompareNumerically
- Subject: Re: kCFCompareNumerically
- From: Jeff Gilbert <email@hidden>
- Date: Tue, 15 Jan 2002 19:56:10 -0600
Hi Steve,
I just wrote a category on NSString to do this the other day. This
version will deal with multiple embedded numbers.
For example, abc12def36 < abc123def9 < abc123def10 < abc124def2.
SmartCompare.h:
@interface NSString(SmartCompare)
- (NSComparisonResult)smartCompare:(NSString*)aString;
@end
SmartCompare.m:
#import "SmartCompare.h"
@implementation NSString(SmartCompare)
- (NSComparisonResult)smartCompare:(NSString*)aString
{
// we assume the strings are equal until proven otherwise
NSComparisonResult result = NSOrderedSame;
NSScanner* scanner1 = [[NSScanner alloc] initWithString:self];
NSScanner* scanner2 = [[NSScanner alloc] initWithString:aString];
NSCharacterSet* numberSet = [NSCharacterSet
decimalDigitCharacterSet];
NSString* sub1; // used to hold the substrings from the scanners
NSString* sub2;
BOOL gotSub1; // did the scanners return a valid substring?
BOOL gotSub2;
BOOL isAtEnd1 = [scanner1 isAtEnd]; // has the scanner finished
BOOL isAtEnd2 = [scanner2 isAtEnd];
while ((result == NSOrderedSame) && !isAtEnd1 && !isAtEnd2)
{
// first, we look for a text substring
gotSub1 = [scanner1 scanUpToCharactersFromSet:numberSet
intoString:&sub1];
gotSub2 = [scanner2 scanUpToCharactersFromSet:numberSet
intoString:&sub2];
if (gotSub1 && gotSub2)
result = [sub1 compare:sub2];
else if (!gotSub1 && gotSub2)
result = NSOrderedAscending;
else if (gotSub1 && !gotSub2)
result = NSOrderedDescending;
isAtEnd1 = [scanner1 isAtEnd];
isAtEnd2 = [scanner2 isAtEnd];
// is the string is still equal, we look for a number substring
if ((result == NSOrderedSame) && !isAtEnd1 && !isAtEnd2)
{
long long number1;
long long number2;
gotSub1 = [scanner1 scanLongLong:&number1];
gotSub2 = [scanner2 scanLongLong:&number2];
if (gotSub1 && gotSub2)
{
if (number1 < number2)
result = NSOrderedAscending;
else if (number1 > number2)
result = NSOrderedDescending;
}
else if (!gotSub1 && gotSub2)
result = NSOrderedAscending;
else if (gotSub1 && !gotSub2)
result = NSOrderedDescending;
isAtEnd1 = [scanner1 isAtEnd];
isAtEnd2 = [scanner2 isAtEnd];
}
}
// when we exit the while loop, at least one of the strings has been
exhausted
// if the strings are still considered equal and only one string has
finished
// then the finished string is the lower string (e.g. he/help)
if (result == NSOrderedSame)
{
if (isAtEnd1 && !isAtEnd2)
result = NSOrderedAscending;
else if (!isAtEnd1 && isAtEnd2)
result = NSOrderedDescending;
}
[scanner1 release];
[scanner2 release];
return result;
}
@end
enjoy,
Jeff Gilbert
On Monday, January 14, 2002, at 12:11 AM, Steve Gehrman wrote:
According to the comments in CFString...
"kCFCompareNumerically is currently ignored in the Compare routines."
Does anyone have code that will compare NSStrings numerically ie:
Foo2.txt < Foo7.txt < Foo25.txt
Thanks!
-steve
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.