From 2dd421fbffccd81f6e8d9aa082d2bcbd390678cc Mon Sep 17 00:00:00 2001 From: qmathe Date: Fri, 14 Feb 2014 13:02:41 +0000 Subject: [PATCH] Added missing Mac OS X 10.6 methods related to NSSortDescriptor git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@37689 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 9 ++++++++ Headers/Foundation/NSSortDescriptor.h | 30 +++++++++++++++++++++++++++ Source/NSSortDescriptor.m | 24 +++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/ChangeLog b/ChangeLog index 1667f6097..56cbfd2ff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2014-02-14 Quentin Mathe + + * Headers/Foundation/NSSortDescriptor.h + * Source/Foundation/NSSortDescriptor.m + (+sortDescriptorWithKey:ascending:selector:, + +sortDescriptorWithKey:ascending, + -[NSSet sortedArrayUsingDescriptors:]): Added missing Mac OS X 10.6 + methods. + 2014-02-13 Richard Frith-Macdonald * Source/GSFormat.m: Fix to cope with cases where a format specifies diff --git a/Headers/Foundation/NSSortDescriptor.h b/Headers/Foundation/NSSortDescriptor.h index bb98c1358..dbe25d137 100644 --- a/Headers/Foundation/NSSortDescriptor.h +++ b/Headers/Foundation/NSSortDescriptor.h @@ -30,6 +30,7 @@ #import #import +#import #if defined(__cplusplus) extern "C" { @@ -74,6 +75,25 @@ extern "C" { */ - (NSComparisonResult) compareObject: (id)object1 toObject: (id)object2; +#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST) +/** Returns an autoreleased sort descriptor for comparisons using the + * 'compare:' selector and the specified key and ordering. + * + * See also -initWithKey:ascending:. + */ ++ (id) sortDescriptorWithKey: (NSString *)aKey ascending: (BOOL)ascending; + +/** Returns an autoreleased sort descriptor initialized to perform comparisons + * in the specified order using aSelector to compare the property aKey of each + * object. + * + * See also -initWithKey:ascending:selector:. + */ ++ (id) sortDescriptorWithKey: (NSString *)aKey + ascending: (BOOL)ascending + selector: (SEL)aSelector; +#endif + /** Initialises the receiver for comparisons using the 'compare:' selector * and the specified key and ordering. */ @@ -129,6 +149,16 @@ extern "C" { @end +#if OS_API_VERSION(MAC_OS_X_VERSION_10_6,GS_API_LATEST) +@interface NSSet (NSSortDescriptorSorting) + /** + * Produces a sorted array from using the mechanism described for + * [NSMutableArray-sortUsingDescriptors:] + */ +- (NSArray *) sortedArrayUsingDescriptors: (NSArray *)sortDescriptors; +@end +#endif + #if defined(__cplusplus) } #endif diff --git a/Source/NSSortDescriptor.m b/Source/NSSortDescriptor.m index 13bcc7492..703062940 100644 --- a/Source/NSSortDescriptor.m +++ b/Source/NSSortDescriptor.m @@ -121,6 +121,20 @@ static BOOL initialized = NO; return _ascending + GSPrivateHash(0, sel, strlen(sel)) + [_key hash]; } ++ (id) sortDescriptorWithKey: (NSString *)aKey ascending: (BOOL)ascending +{ + return AUTORELEASE([[self alloc] initWithKey: aKey ascending: ascending]); +} + ++ (id) sortDescriptorWithKey: (NSString *)aKey + ascending: (BOOL)ascending + selector: (SEL)aSelector +{ + return AUTORELEASE([[self alloc] initWithKey: aKey + ascending: ascending + selector: aSelector]); +} + - (id) initWithKey: (NSString *) key ascending: (BOOL) ascending { return [self initWithKey: key ascending: ascending selector: NULL]; @@ -449,3 +463,13 @@ SortRange(id *objects, NSRange range, id *descriptors, } @end + +@implementation NSSet (NSSortDescriptorSorting) + +- (NSArray *) sortedArrayUsingDescriptors: (NSArray *)sortDescriptors +{ + return [[self allObjects] sortedArrayUsingDescriptors: sortDescriptors]; +} + +@end +