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