/*
Objective-c - block作为方法的参数:字符串数组的排序
两个字符串的比较由方法的调用者传入.
*/
#import <Foundation/Foundation.h>
#import "YYArray.h"
int main(int argc, const char * argv[]) {
//定义字符串数组并初始化
char *names[] = {
"xuezhangjun",
"zuoxiaoying",
"liyajie",
"guojianjun",
"liuxuemei"
};
//创建array对象
YYArray *array = [YYArray new];
//计算字符串数组的长度
int length = sizeof names / sizeof names[0];
//1,调用排序的方法,传入的block要求:按字符串数组内元素的长度排序
[array compositorWithArray:names andLength:length andBlock:^BOOL(char *name1, char *name2){
size_t len1 = strlen(name1);
size_t len2 = strlen(name2);
if((int)len1 - (int)len2 > 0)
{
return YES;
}
return NO;
}];
//输出
for(int i = 0; i < length; i++)
{
NSLog(@"%s", names[i]);
}
//分隔线
NSLog(@"----分隔线----");
NewType b1 = ^BOOL(char *name1, char *name2){
int res = strcmp(name1, name2);
if(res > 0)
{
return YES;
}
return NO;
};
//2,调用方法,传入排序要求block类型的变量b1,按姓名首字母的ASCII码排序
[array compositorWithArray:names andLength:length andBlock:b1];
//输出
for(int i = 0; i < length; i++)
{
NSLog(@"%s", names[i]);
}
return 0;
}