4 Copyright (C) 1996 Free Software Foundation, Inc.
6 Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>
9 This file is part of the GNUstep Database Library.
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Library General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Library General Public License for more details.
21 You should have received a copy of the GNU Library General Public
22 License along with this library; see the file COPYING.LIB.
23 If not, write to the Free Software Foundation,
24 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #import <Foundation/NSAutoreleasePool.h>
28 #import <Foundation/NSArray.h>
29 #import <Foundation/NSDictionary.h>
30 #import <Foundation/NSString.h>
32 #import "EOKeySortOrdering.h"
33 #import <EOControl/EOKeyValueCoding.h>
35 @implementation EOKeySortOrdering
37 + keyOrderingWithKey:(NSString*)aKey ordering:(NSComparisonResult)anOrdering
39 return AUTORELEASE([[EOKeySortOrdering alloc]
40 initWithKey:aKey ordering:anOrdering]);
43 - initWithKey:(NSString*)aKey ordering:(NSComparisonResult)anOrdering
46 ordering = anOrdering;
50 - (NSString*)key {return key;}
51 - (NSComparisonResult)ordering {return ordering;}
55 // TODO : integrate this function in the two methods above and optimize
56 // object creation and method calls for objects that provide quick
57 // access to their values - do not use nested functions
59 static NSComparisonResult _keySortCompare(id obj1, id obj2, NSArray* order)
60 __attribute__((unused));
62 static NSComparisonResult _keySortCompare(id obj1, id obj2, NSArray* order) {
65 for (i = 0, n = [order count]; i < n; i++) {
66 id val1, val2, key, kar;
67 NSComparisonResult ord, vord;
68 EOKeySortOrdering* kso = [order objectAtIndex:i];
72 kar = [NSArray arrayWithObject:key];
74 val1 = [[obj1 valuesForKeys:kar] objectForKey:key];
75 val2 = [[obj2 valuesForKeys:kar] objectForKey:key];
81 return ord == NSOrderedAscending ?
82 NSOrderedAscending : NSOrderedDescending;
85 return ord == NSOrderedAscending ?
86 NSOrderedDescending : NSOrderedAscending;
88 vord = [(NSString *)val1 compare:val2];
90 if (vord == NSOrderedSame)
93 if (vord == NSOrderedAscending)
94 return ord == NSOrderedAscending ?
95 NSOrderedAscending : NSOrderedDescending;
97 return ord == NSOrderedAscending ?
98 NSOrderedDescending : NSOrderedAscending;
101 return NSOrderedSame;
106 @implementation NSArray(EOKeyBasedSorting)
108 - (NSArray*)sortedArrayUsingKeyOrderArray:(NSArray*)orderArray
111 CREATE_AUTORELEASE_POOL(pool);
113 arry = [self sortedArrayUsingFunction:
114 (int(*)(id, id, void*))_keySortCompare
122 @implementation NSMutableArray(EOKeyBasedSorting)
124 - (void)sortUsingKeyOrderArray:(NSArray*)orderArray
126 CREATE_AUTORELEASE_POOL(pool);
128 [self sortUsingFunction:
129 (int(*)(id, id, void*))_keySortCompare