From 202f0ceed2f6be3bb3e6e1e3b6d31aade3c3b5d8 Mon Sep 17 00:00:00 2001 From: Gregory John Casamento Date: Fri, 2 Aug 2019 05:48:50 -0400 Subject: [PATCH 1/6] Add declration to header for NSSortDescriptor sortDescriptorWithKey:ascending:comparator: --- Headers/Foundation/NSSortDescriptor.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Headers/Foundation/NSSortDescriptor.h b/Headers/Foundation/NSSortDescriptor.h index 1f3d608c2..2108de595 100644 --- a/Headers/Foundation/NSSortDescriptor.h +++ b/Headers/Foundation/NSSortDescriptor.h @@ -92,6 +92,16 @@ extern "C" { + (id) sortDescriptorWithKey: (NSString *)aKey ascending: (BOOL)ascending selector: (SEL)aSelector; + +/**

Returns an autoreleased sort descriptor initialized to perform + * comparisons in the specified order using the comparator to compare + * the property aKey of each object.

+ * + *

See also -initWithKey:ascending:selector:.

+ */ ++ (id)sortDescriptorWithKey: (NSString *)key + ascending: (BOOL)ascending + comparator: (NSComparator)cmptr; #endif /** Initialises the receiver for comparisons using the 'compare:' selector From 64a59aeb084b52264db2cb55b3c2e18abaea857d Mon Sep 17 00:00:00 2001 From: Gregory John Casamento Date: Fri, 2 Aug 2019 09:30:39 -0400 Subject: [PATCH 2/6] Initial implementation --- Source/NSSortDescriptor.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Source/NSSortDescriptor.m b/Source/NSSortDescriptor.m index a2cf1e0f6..eaf43c2dd 100644 --- a/Source/NSSortDescriptor.m +++ b/Source/NSSortDescriptor.m @@ -167,6 +167,13 @@ static BOOL initialized = NO; selector: aSelector]); } ++ (id)sortDescriptorWithKey: (NSString *)key + ascending: (BOOL)ascending + comparator: (NSComparator)cmptr +{ + return nil; +} + - (id) initWithKey: (NSString *) key ascending: (BOOL) ascending { return [self initWithKey: key ascending: ascending selector: NULL]; From adbd44470f963de7bcaad48cc0e9f4fadfdad104 Mon Sep 17 00:00:00 2001 From: Gregory John Casamento Date: Fri, 2 Aug 2019 09:41:55 -0400 Subject: [PATCH 3/6] Add comparator to NSSortDescriptor --- Headers/Foundation/NSSortDescriptor.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Headers/Foundation/NSSortDescriptor.h b/Headers/Foundation/NSSortDescriptor.h index 2108de595..023782388 100644 --- a/Headers/Foundation/NSSortDescriptor.h +++ b/Headers/Foundation/NSSortDescriptor.h @@ -50,6 +50,7 @@ extern "C" { NSString *_key; BOOL _ascending; SEL _selector; + NSComparator _comparator; #endif #if GS_NONFRAGILE #else From bdc589ce79414ab27290b60b7e739bc5c59b68cb Mon Sep 17 00:00:00 2001 From: Gregory John Casamento Date: Fri, 2 Aug 2019 10:58:13 -0400 Subject: [PATCH 4/6] Use comparator in sorting --- Headers/Foundation/NSSortDescriptor.h | 11 ++++- Source/NSSortDescriptor.m | 63 ++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/Headers/Foundation/NSSortDescriptor.h b/Headers/Foundation/NSSortDescriptor.h index 023782388..763dee16b 100644 --- a/Headers/Foundation/NSSortDescriptor.h +++ b/Headers/Foundation/NSSortDescriptor.h @@ -103,6 +103,14 @@ extern "C" { + (id)sortDescriptorWithKey: (NSString *)key ascending: (BOOL)ascending comparator: (NSComparator)cmptr; + +/** + * Initialises the receiver to perform comparisons in the specified order + * using the comparator to compare the property key of each object. + */ +- (id) initWithKey: (NSString *)key + ascending: (BOOL)ascending + comparator: (NSComparator)cmptr; #endif /** Initialises the receiver for comparisons using the 'compare:' selector @@ -113,12 +121,13 @@ extern "C" { /** * Initialises the receiver to perform comparisons in the specified order - * using selector to compar the property key of each object. + * using selector to compare the property key of each object. */ - (id) initWithKey: (NSString *)key ascending: (BOOL)ascending selector: (SEL)selector; + /** Returns the key used to obtain the property on which comparisons are based. */ - (NSString *) key; diff --git a/Source/NSSortDescriptor.m b/Source/NSSortDescriptor.m index eaf43c2dd..b1aa77a73 100644 --- a/Source/NSSortDescriptor.m +++ b/Source/NSSortDescriptor.m @@ -113,8 +113,16 @@ static BOOL initialized = NO; id comparedKey1 = [object1 valueForKeyPath: _key]; id comparedKey2 = [object2 valueForKeyPath: _key]; - result = (NSComparisonResult) [comparedKey1 performSelector: _selector - withObject: comparedKey2]; + if(_comparator != NULL) + { + result = CALL_BLOCK(((NSComparator)_comparator), comparedKey1, comparedKey2); + } + else + { + result = (NSComparisonResult) [comparedKey1 performSelector: _selector + withObject: comparedKey2]; + } + if (_ascending == NO) { if (result == NSOrderedAscending) @@ -132,12 +140,23 @@ static BOOL initialized = NO; - (id) copyWithZone: (NSZone*)zone { + NSSortDescriptor *copy = nil; if (NSShouldRetainWithZone(self, zone)) { return RETAIN(self); } - return [[NSSortDescriptor allocWithZone: zone] - initWithKey: _key ascending: _ascending selector: _selector]; + + if(_comparator == NULL) + { + copy = [[NSSortDescriptor allocWithZone: zone] + initWithKey: _key ascending: _ascending selector: _selector]; + } + else + { + copy = [[NSSortDescriptor allocWithZone: zone] + initWithKey: _key ascending: _ascending comparator: _comparator]; + } + return copy; } - (void) dealloc @@ -171,7 +190,10 @@ static BOOL initialized = NO; ascending: (BOOL)ascending comparator: (NSComparator)cmptr { - return nil; + return AUTORELEASE([[self alloc] initWithKey: key + ascending: ascending + comparator: cmptr]); + } - (id) initWithKey: (NSString *) key ascending: (BOOL) ascending @@ -179,6 +201,37 @@ static BOOL initialized = NO; return [self initWithKey: key ascending: ascending selector: NULL]; } +- (id) initWithKey: (NSString *) key + ascending: (BOOL) ascending + comparator: (NSComparator) cmptr +{ + if ([self init]) + { + if (key == nil) + { + [NSException raise: NSInvalidArgumentException + format: @"%@", _(@"Passed nil key when initializing " + @"an NSSortDescriptor.")]; + } + if (cmptr == NULL) + { + [NSException raise: NSInvalidArgumentException + format: @"%@", _(@"Passed NULL comparator when initializing " + @"an NSSortDescriptor.")]; + } + + ASSIGN(_key, key); + _ascending = ascending; + _comparator = cmptr; + + return self; + } + else + { + return nil; + } +} + - (id) initWithKey: (NSString *) key ascending: (BOOL) ascending selector: (SEL) selector From 93b4fed381d346ce220c431aca9007d0b8bb22e7 Mon Sep 17 00:00:00 2001 From: Gregory John Casamento Date: Sun, 4 Aug 2019 22:36:10 -0400 Subject: [PATCH 5/6] Add retain and release for block --- Source/NSSortDescriptor.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/NSSortDescriptor.m b/Source/NSSortDescriptor.m index b1aa77a73..ba8a6f494 100644 --- a/Source/NSSortDescriptor.m +++ b/Source/NSSortDescriptor.m @@ -162,6 +162,7 @@ static BOOL initialized = NO; - (void) dealloc { TEST_RELEASE(_key); + TEST_RELEASE(_comparator); [super dealloc]; } @@ -222,7 +223,7 @@ static BOOL initialized = NO; ASSIGN(_key, key); _ascending = ascending; - _comparator = cmptr; + ASSIGN(_comparator, cmptr); return self; } From a90a55a3eab60030eb4634e2f3e671b9c2b47ab9 Mon Sep 17 00:00:00 2001 From: Gregory John Casamento Date: Mon, 5 Aug 2019 10:20:40 -0400 Subject: [PATCH 6/6] Fix suggestions made by Fred --- Source/NSSortDescriptor.m | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/NSSortDescriptor.m b/Source/NSSortDescriptor.m index ba8a6f494..27e8bb575 100644 --- a/Source/NSSortDescriptor.m +++ b/Source/NSSortDescriptor.m @@ -113,15 +113,15 @@ static BOOL initialized = NO; id comparedKey1 = [object1 valueForKeyPath: _key]; id comparedKey2 = [object2 valueForKeyPath: _key]; - if(_comparator != NULL) - { - result = CALL_BLOCK(((NSComparator)_comparator), comparedKey1, comparedKey2); - } - else + if (_comparator == NULL) { result = (NSComparisonResult) [comparedKey1 performSelector: _selector withObject: comparedKey2]; } + else + { + result = CALL_BLOCK(((NSComparator)_comparator), comparedKey1, comparedKey2); + } if (_ascending == NO) { @@ -146,7 +146,7 @@ static BOOL initialized = NO; return RETAIN(self); } - if(_comparator == NULL) + if (_comparator == NULL) { copy = [[NSSortDescriptor allocWithZone: zone] initWithKey: _key ascending: _ascending selector: _selector];