all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
#define esign(X) (((X)>0)?1:(((X)<0)?-1:0))
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
NSMutableArray *arr =
|
||||
[NSMutableArray
|
||||
arrayWithArray: [@"this is a set of strings to sort"
|
||||
componentsSeparatedByString: @" "]
|
||||
];
|
||||
|
||||
[arr sortUsingComparator: ^NSComparisonResult(id obj1, id obj2){
|
||||
NSComparisonResult l = esign((int)([obj1 length] - [obj2 length]));
|
||||
return l ? -l // reverse the ordering
|
||||
: [obj1 caseInsensitiveCompare: obj2];
|
||||
}];
|
||||
|
||||
for( NSString *str in arr )
|
||||
{
|
||||
NSLog(@"%@", str);
|
||||
}
|
||||
|
||||
[pool release];
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSString (CustomComp)
|
||||
- (NSComparisonResult)my_compare: (id)obj;
|
||||
@end
|
||||
|
||||
#define esign(X) (((X)>0)?1:(((X)<0)?-1:0))
|
||||
@implementation NSString (CustomComp)
|
||||
- (NSComparisonResult)my_compare: (id)obj
|
||||
{
|
||||
NSComparisonResult l = esign((int)([self length] - [obj length]));
|
||||
return l ? -l // reverse the ordering
|
||||
: [self caseInsensitiveCompare: obj];
|
||||
}
|
||||
@end
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
NSMutableArray *arr =
|
||||
[NSMutableArray
|
||||
arrayWithArray: [@"this is a set of strings to sort"
|
||||
componentsSeparatedByString: @" "]
|
||||
];
|
||||
|
||||
[arr sortUsingSelector: @selector(my_compare:)];
|
||||
|
||||
NSEnumerator *iter = [arr objectEnumerator];
|
||||
NSString *str;
|
||||
while( (str = [iter nextObject]) != nil )
|
||||
{
|
||||
NSLog(@"%@", str);
|
||||
}
|
||||
|
||||
[pool release];
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
NSArray *strings = [@"Here are some sample strings to be sorted" componentsSeparatedByString:@" "];
|
||||
|
||||
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"length" ascending:NO];
|
||||
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"lowercaseString" ascending:YES];
|
||||
|
||||
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
|
||||
[sd1 release];
|
||||
[sd2 release];
|
||||
|
||||
NSArray *sorted = [strings sortedArrayUsingDescriptors:sortDescriptors];
|
||||
NSLog(@"%@", sorted);
|
||||
|
||||
[pool release];
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue