Hi all,
Following along in the documentation using "sortedArrayUsingFunction", I cannot seem to to be able to get the Array to sort as I expect.
So, here is the code...in a command line ap for simplicity. I would have expected the last 2 sorts to be inverse of the other, but both sorts are the same.
I **think** I have pretty carefully copied the examples from the docs...so would really like to get some insight.
Thanks as always.
Following along in the documentation using "sortedArrayUsingFunction", I cannot seem to to be able to get the Array to sort as I expect.
So, here is the code...in a command line ap for simplicity. I would have expected the last 2 sorts to be inverse of the other, but both sorts are the same.
I **think** I have pretty carefully copied the examples from the docs...so would really like to get some insight.
Thanks as always.
Code:
#import <Foundation/Foundation.h>
NSInteger alphabeticSort(id string1, id string2, void *reverse)
{
if (( NSInteger *) reverse == NO)
{
return [string2 localizedCaseInsensitiveCompare:string1];
}
return [string1 localizedCaseInsensitiveCompare:string2];
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *anArray =
[NSMutableArray arrayWithObjects:@ "i", @"a", @"d", @"b", @"c", @"e",@"g",@"L", nil];
NSLog(@"anArray:%@", anArray);
NSArray *sortedArray;
int reverseSort = NO;
sortedArray = [anArray sortedArrayUsingFunction:alphabeticSort context:&reverseSort];
NSLog(@"Sort using function: \"NO\" %@", sortedArray);
reverseSort = YES;
sortedArray = [anArray sortedArrayUsingFunction:alphabeticSort context:&reverseSort];
NSLog(@"Sort using function:\"YES\" %@", sortedArray);
[pool drain];
return 0;
}
output:
2010-03-18 10:37:45.268 SortFun[2574:a0f] anArray:
i,
a,
d,
b,
c,
e,
g,
L
)
2010-03-18 10:37:45.273 SortFun[2574:a0f] Sort using function: "NO" (
a,
b,
c,
d,
e,
g,
i,
L
)
2010-03-18 10:37:45.273 SortFun[2574:a0f] Sort using function:"YES" (
a,
b,
c,
d,
e,
g,
i,
L
)