API  0.9.7
 All Classes Files Functions Variables Macros Groups Pages
CPSortDescriptor.j
Go to the documentation of this file.
1 /*
2  * CPSortDescriptor.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 @class CPString
25 
35 @implementation CPSortDescriptor : CPObject
36 {
37  CPString _key;
38  SEL _selector;
39  BOOL _ascending;
40 }
41 
42 + (id)sortDescriptorWithKey:(CPString)aKey ascending:(BOOL)isAscending
43 {
44  return [[self alloc] initWithKey:aKey ascending:isAscending];
45 }
46 
47 // Initializing a sort descriptor
54 - (id)initWithKey:(CPString)aKey ascending:(BOOL)isAscending
55 {
56  return [self initWithKey:aKey ascending:isAscending selector:@selector(compare:)];
57 }
58 
59 + (id)sortDescriptorWithKey:(CPString)aKey ascending:(BOOL)isAscending selector:(SEL)aSelector
60 {
61  return [[self alloc] initWithKey:aKey ascending:isAscending selector:aSelector];
62 }
63 
71 - (id)initWithKey:(CPString)aKey ascending:(BOOL)isAscending selector:(SEL)aSelector
72 {
73  self = [super init];
74 
75  if (self)
76  {
77  _key = aKey;
78  _ascending = isAscending;
79  _selector = aSelector;
80  }
81 
82  return self;
83 }
84 
85 // Getting information about a sort descriptor
89 - (BOOL)ascending
90 {
91  return _ascending;
92 }
93 
97 - (CPString)key
98 {
99  return _key;
100 }
101 
105 - (SEL)selector
106 {
107  return _selector;
108 }
109 
110 // Using sort descriptors
117 - (CPComparisonResult)compareObject:(id)lhsObject withObject:(id)rhsObject
118 {
119  return (_ascending ? 1 : -1) * [[lhsObject valueForKeyPath:_key] performSelector:_selector withObject:[rhsObject valueForKeyPath:_key]];
120 }
121 
126 - (id)reversedSortDescriptor
127 {
128  return [[[self class] alloc] initWithKey:_key ascending:!_ascending selector:_selector];
129 }
130 
131 - (CPString)description
132 {
133  return [CPString stringWithFormat:@"(%@, %@, %@)",
134  [self key], [self ascending] ? @"ascending": @"descending", CPStringFromSelector([self selector])];
135 }
136 
137 @end
138 
139 var CPSortDescriptorKeyKey = @"CPSortDescriptorKeyKey", // Don't you just love naming schemes ;)
140  CPSortDescriptorAscendingKey = @"CPSortDescriptorAscendingKey",
141  CPSortDescriptorSelectorKey = @"CPSortDescriptorSelectorKey";
142 
143 @implementation CPSortDescriptor (CPCoding)
144 
145 - (id)initWithCoder:(CPCoder)aCoder
146 {
147  if (self = [super init])
148  {
149  _key = [aCoder decodeObjectForKey:CPSortDescriptorKeyKey];
150  _ascending = [aCoder decodeBoolForKey:CPSortDescriptorAscendingKey];
151  _selector = CPSelectorFromString([aCoder decodeObjectForKey:CPSortDescriptorSelectorKey]);
152  }
153 
154  return self;
155 }
156 
157 - (void)encodeWithCoder:(CPCoder)aCoder
158 {
159  [aCoder encodeObject:_key forKey:CPSortDescriptorKeyKey];
160  [aCoder encodeBool:_ascending forKey:CPSortDescriptorAscendingKey];
161  [aCoder encodeObject:CPStringFromSelector(_selector) forKey:CPSortDescriptorSelectorKey];
162 }
163 
164 @end