1 /* Copyright (c) 2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <Foundation/NSSortDescriptor.h>
10 #import <Foundation/NSString.h>
11 #import <Foundation/NSKeyValueCoding.h>
12 #import <Foundation/NSRaise.h>
13 #import <Foundation/NSCoder.h>
14 #import <Foundation/NSKeyedUnarchiver.h>
16 @implementation NSSortDescriptor
18 +sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending {
19 return [[[NSSortDescriptor allocWithZone:NULL] initWithKey:key ascending:ascending selector:NULL] autorelease];
22 +sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending selector:(SEL)selector {
23 return [[[NSSortDescriptor allocWithZone:NULL] initWithKey:key ascending:ascending selector:selector] autorelease];
26 -initWithKey:(NSString *)key ascending:(BOOL)ascending {
27 return [self initWithKey:key ascending:ascending selector:NULL];
30 -initWithKey:(NSString *)key ascending:(BOOL)ascending selector:(SEL)selector {
34 if(selector==NULL) // Yes it does this
35 _selector=@selector(compare:);
46 -copyWithZone:(NSZone *)zone {
62 -(NSComparisonResult)compareObject:first toObject:second {
63 id checkFirst=[first valueForKeyPath:_key];
64 id checkSecond=[second valueForKeyPath:_key];
65 NSComparisonResult result;
68 result=(NSComparisonResult)[checkFirst performSelector:_selector withObject:checkSecond];
70 result=(NSComparisonResult)[checkSecond performSelector:_selector withObject:checkFirst];
75 -reversedSortDescriptor {
76 return [[[isa alloc] initWithKey:_key ascending:!_ascending selector:_selector] autorelease];
79 - (void)encodeWithCoder:(NSCoder *)coder {
80 if ([coder allowsKeyedCoding])
82 [coder encodeObject:_key forKey:@"Key"];
83 [coder encodeBool:_ascending forKey:@"Ascending"];
84 [coder encodeObject:NSStringFromSelector(_selector) forKey: @"Selector"];
88 [coder encodeObject:_key];
89 [coder encodeValueOfObjCType:@encode(BOOL) at:&_ascending];
90 [coder encodeObject:NSStringFromSelector(_selector)];
94 - (id)initWithCoder:(NSCoder *)coder {
95 if ([coder allowsKeyedCoding])
97 NSKeyedUnarchiver *keyed = (NSKeyedUnarchiver *)coder;
98 _key = [[keyed decodeObjectForKey:@"Key"] copy];
99 _ascending = [keyed decodeBoolForKey:@"Ascending"];
100 _selector = NSSelectorFromString([keyed decodeObjectForKey:@"Selector"]);
105 _key = [[coder decodeObject] copy];
106 [coder decodeValueOfObjCType:@encode(BOOL) at:&_ascending];
107 _selector = NSSelectorFromString([coder decodeObject]);