00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPObject.j"
00024 @import "CPObjJRuntime.j"
00025
00026
00032 CPOrderedAscending = -1;
00038 CPOrderedSame = 0;
00044 CPOrderedDescending = 1;
00045
00055 @implementation CPSortDescriptor : CPObject
00056 {
00057 CPString _key;
00058 SEL _selector;
00059 BOOL _ascending;
00060 }
00061
00062 + (id)sortDescriptorWithKey:(CPString)aKey ascending:(BOOL)isAscending
00063 {
00064 return [[self alloc] initWithKey:aKey ascending:isAscending];
00065 }
00066
00067
00074 - (id)initWithKey:(CPString)aKey ascending:(BOOL)isAscending
00075 {
00076 return [self initWithKey:aKey ascending:isAscending selector:@selector(compare:)];
00077 }
00078
00079 + (id)sortDescriptorWithKey:(CPString)aKey ascending:(BOOL)isAscending selector:(SEL)aSelector
00080 {
00081 return [[self alloc] initWithKey:aKey ascending:isAscending selector:aSelector];
00082 }
00083
00091 - (id)initWithKey:(CPString)aKey ascending:(BOOL)isAscending selector:(SEL)aSelector
00092 {
00093 self = [super init];
00094
00095 if (self)
00096 {
00097 _key = aKey;
00098 _ascending = isAscending;
00099 _selector = aSelector;
00100 }
00101
00102 return self;
00103 }
00104
00105
00109 - (BOOL)ascending
00110 {
00111 return _ascending;
00112 }
00113
00117 - (CPString)key
00118 {
00119 return _key;
00120 }
00121
00125 - (SEL)selector
00126 {
00127 return _selector;
00128 }
00129
00130
00137 - (CPComparisonResult)compareObject:(id)lhsObject withObject:(id)rhsObject
00138 {
00139 return (_ascending ? 1 : -1) * [[lhsObject valueForKeyPath:_key] performSelector:_selector withObject:[rhsObject valueForKeyPath:_key]];
00140 }
00141
00146 - (id)reversedSortDescriptor
00147 {
00148 return [[[self class] alloc] initWithKey:_key ascending:!_ascending selector:_selector];
00149 }
00150
00151 @end