NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 10; i ++) {
[NSThread sleepForTimeInterval:1.f];
Test23 *test = [[Test23 alloc] init];
test.name = [NSString stringWithFormat:@"name_%d",i];
test.date = [NSDate date];
[array addObject:test];
[test release];
}
for (Test23 *test in array) {
NSLog(@"%@",test.name);
NSLog(@"%f",test.date.timeIntervalSince1970);
}
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Test23 *test1 = obj1;
Test23 *test2 = obj2;
double a = test1.date.timeIntervalSince1970;
double b = test2.date.timeIntervalSince1970;
if (a < b) {
return NSOrderedDescending;
} else if (a > b) {
return NSOrderedAscending;
} else {
return NSOrderedSame;
}
}];
NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
for (Test23 *test in array) {
NSLog(@"%@",test.name);
NSLog(@"%f",test.date.timeIntervalSince1970);
}
for (int i = 0; i < 10; i ++) {
[NSThread sleepForTimeInterval:1.f];
Test23 *test = [[Test23 alloc] init];
test.name = [NSString stringWithFormat:@"name_%d",i];
test.date = [NSDate date];
[array addObject:test];
[test release];
}
for (Test23 *test in array) {
NSLog(@"%@",test.name);
NSLog(@"%f",test.date.timeIntervalSince1970);
}
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Test23 *test1 = obj1;
Test23 *test2 = obj2;
double a = test1.date.timeIntervalSince1970;
double b = test2.date.timeIntervalSince1970;
if (a < b) {
return NSOrderedDescending;
} else if (a > b) {
return NSOrderedAscending;
} else {
return NSOrderedSame;
}
}];
NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
for (Test23 *test in array) {
NSLog(@"%@",test.name);
NSLog(@"%f",test.date.timeIntervalSince1970);
}