42 + (id)sortDescriptorWithKey:(
CPString)aKey ascending:(BOOL)isAscending
44 return [[
self alloc] initWithKey:aKey ascending:isAscending];
54 - (id)initWithKey:(
CPString)aKey ascending:(BOOL)isAscending
56 return [
self initWithKey:aKey ascending:isAscending selector:@selector(compare:)];
59 + (id)sortDescriptorWithKey:(
CPString)aKey ascending:(BOOL)isAscending selector:(
SEL)aSelector
61 return [[
self alloc] initWithKey:aKey ascending:isAscending selector:aSelector];
71 - (id)initWithKey:(
CPString)aKey ascending:(BOOL)isAscending selector:(
SEL)aSelector
78 _ascending = isAscending;
79 _selector = aSelector;
117 - (CPComparisonResult)compareObject:(
id)lhsObject withObject:(
id)rhsObject
119 return (_ascending ? 1 : -1) * [[lhsObject valueForKeyPath:_key] performSelector:_selector withObject:[rhsObject valueForKeyPath:_key]];
126 - (id)reversedSortDescriptor
128 return [[[
self class] alloc] initWithKey:_key ascending:!_ascending selector:_selector];
133 return [
CPString stringWithFormat:@"(%@, %@, %@)",
134 [
self key], [
self ascending] ? @"ascending": @"descending", CPStringFromSelector([
self selector])];
139 var CPSortDescriptorKeyKey =
@"CPSortDescriptorKeyKey",
140 CPSortDescriptorAscendingKey =
@"CPSortDescriptorAscendingKey",
141 CPSortDescriptorSelectorKey =
@"CPSortDescriptorSelectorKey";
143 @implementation CPSortDescriptor (CPCoding)
145 - (id)initWithCoder:(
CPCoder)aCoder
147 if (
self = [super init])
149 _key = [aCoder decodeObjectForKey:CPSortDescriptorKeyKey];
150 _ascending = [aCoder decodeBoolForKey:CPSortDescriptorAscendingKey];
157 - (void)encodeWithCoder:(
CPCoder)aCoder
159 [aCoder encodeObject:_key forKey:CPSortDescriptorKeyKey];
160 [aCoder encodeBool:_ascending forKey:CPSortDescriptorAscendingKey];
161 [aCoder encodeObject:CPStringFromSelector(_selector) forKey:CPSortDescriptorSelectorKey];