API  0.9.10
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 
33 @implementation CPSortDescriptor : CPObject
34 {
35  CPString _key;
36  SEL _selector;
37  BOOL _ascending;
38 }
39 
40 + (id)sortDescriptorWithKey:(CPString)aKey ascending:(BOOL)isAscending
41 {
42  return [[self alloc] initWithKey:aKey ascending:isAscending];
43 }
44 
45 // Initializing a sort descriptor
52 - (id)initWithKey:(CPString)aKey ascending:(BOOL)isAscending
53 {
54  return [self initWithKey:aKey ascending:isAscending selector:@selector(compare:)];
55 }
56 
57 + (id)sortDescriptorWithKey:(CPString)aKey ascending:(BOOL)isAscending selector:(SEL)aSelector
58 {
59  return [[self alloc] initWithKey:aKey ascending:isAscending selector:aSelector];
60 }
61 
69 - (id)initWithKey:(CPString)aKey ascending:(BOOL)isAscending selector:(SEL)aSelector
70 {
71  self = [super init];
72 
73  if (self)
74  {
75  _key = aKey;
76  _ascending = isAscending;
77  _selector = aSelector;
78  }
79 
80  return self;
81 }
82 
83 // Getting information about a sort descriptor
87 - (BOOL)ascending
88 {
89  return _ascending;
90 }
91 
96 {
97  return _key;
98 }
99 
103 - (SEL)selector
104 {
105  return _selector;
106 }
107 
108 // Using sort descriptors
115 - (CPComparisonResult)compareObject:(id)lhsObject withObject:(id)rhsObject
116 {
117  return (_ascending ? 1 : -1) * [[lhsObject valueForKeyPath:_key] performSelector:_selector withObject:[rhsObject valueForKeyPath:_key]];
118 }
119 
125 {
126  return [[[self class] alloc] initWithKey:_key ascending:!_ascending selector:_selector];
127 }
128 
130 {
131  return [CPString stringWithFormat:@"(%@, %@, %@)",
132  [self key], [self ascending] ? @"ascending": @"descending", CPStringFromSelector([self selector])];
133 }
134 
135 @end
136 
137 var CPSortDescriptorKeyKey = @"CPSortDescriptorKeyKey", // Don't you just love naming schemes ;)
138  CPSortDescriptorAscendingKey = @"CPSortDescriptorAscendingKey",
139  CPSortDescriptorSelectorKey = @"CPSortDescriptorSelectorKey";
140 
142 
143 - (id)initWithCoder:(CPCoder)aCoder
144 {
145  if (self = [super init])
146  {
147  _key = [aCoder decodeObjectForKey:CPSortDescriptorKeyKey];
148  _ascending = [aCoder decodeBoolForKey:CPSortDescriptorAscendingKey];
149  _selector = CPSelectorFromString([aCoder decodeObjectForKey:CPSortDescriptorSelectorKey]);
150  }
151 
152  return self;
153 }
154 
155 - (void)encodeWithCoder:(CPCoder)aCoder
156 {
157  [aCoder encodeObject:_key forKey:CPSortDescriptorKeyKey];
158  [aCoder encodeBool:_ascending forKey:CPSortDescriptorAscendingKey];
159  [aCoder encodeObject:CPStringFromSelector(_selector) forKey:CPSortDescriptorSelectorKey];
160 }
161 
162 @end
var CPSortDescriptorSelectorKey
function CPSelectorFromString(aSelectorName)
Definition: CPObjJRuntime.j:28
var CPSortDescriptorAscendingKey
An immutable string (collection of characters).
Definition: CPString.h:2
Holds attributes necessary to describe how to sort a set of objects.
id initWithKey:ascending:(CPString aKey, [ascending] BOOL isAscending)
var CPSortDescriptorKeyKey
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
id init()
Definition: CPObject.j:145
id initWithKey:ascending:selector:(CPString aKey, [ascending] BOOL isAscending, [selector] SEL aSelector)
Class class()
Definition: CPObject.j:179
id alloc()
Definition: CPObject.j:130
id stringWithFormat:(CPString format, [,] ...)
Definition: CPString.j:166