NSArray *arr1 = [[NSArray alloc] initWithObjects:@"小敏",@"小飞飞",@"小俊珍", nil];
NSArray *arr2 = [[NSArray alloc] initWithObjects:@"小黑",@"大白",@"胖墩", nil];
NSArray *arr3 = [[NSArray alloc] initWithObjects:@"啦啦",@"哈哈",@"嘎嘎",@"嘿嘿", nil];
NSArray *arr4 = [[NSArray alloc] initWithObjects:@"大家",@"你呢",@"但是",@"反而", @"fennea",nil];
NSMutableDictionary *team = [[NSMutableDictionary alloc] initWithObjectsAndKeys:arr1, @"第一组",arr2,@"第二组",arr3,@"第三组",arr4,@"第四组",nil];
NSArray *class = team.allKeys;
for (int i = 0; i < class.count; i++) {
NSArray *class1 = [team valueForKey:class[i]];
for (int j = 0; j < class1.count; j++) {
NSLog(@"mdicKey1-Value1= %@ -->%@",class[i],class1[j]);
}
}
2.定义一个字典,字典是一个学生的信息:姓名、年龄、学号、地址.通过遍历显示学生的所有信息
NSArray *stu11 = [[NSArray alloc] initWithObjects:@"小飞飞",@"女",@"11",@"1",@"洛阳", nil];
NSArray *stu22 = [[NSArray alloc] initWithObjects:@"妖怪",@"男",@"10" @"6",@"迷失之城",nil];
NSArray *stu33 = [[NSArray alloc] initWithObjects:@"神仙",@"男",@"13",@"3",@"昆仑山", nil];
NSArray *stu44 = [[NSArray alloc] initWithObjects:@"小敏",@"女" ,@"18",@"4",@"周口", nil];
NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:stu11,@"1",stu22,@"2",stu33,@"3",stu44,@"4", nil];
NSArray *key1 = message.allKeys;
for (int i = 0; i < key1.count; i++) {
NSArray *value1 = [message valueForKey:key1[i]];
for (int j = 0; j < value1.count; j++) {
NSLog(@"%@ ->%@",key1[i],value1[j]);
}
}
3.定义一个数组,数组中是student对象,按学生的 学号/年龄/姓名 排序
Student *stu1 = [Student studentWithName:@"xiao小飞飞" withSex:@"女" age:12 number:1 adress:@"洛阳"];
Student *stu2 = [Student studentWithName:@"Yao妖怪" withSex:@"男" age:19 number:3 adress:@"迷失之城"];
Student *stu3 = [Student studentWithName:@"Shen神仙" withSex:@"男" age:16 number:2 adress:@"昆仑山"];
Student *stu4 = [Student studentWithName:@"Min小敏" withSex:@"女" age:13 number:7 adress:@"周口"];
NSMutableArray *Array = [NSMutableArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
NSLog(@"----------------------------------按年龄进行排序");
for (int i = 0; i < Array.count - 1; i++) {
for (int j = 0; j < Array.count - 1 - i; j++) {
if ([Array[j] age] > [Array[j + 1] age]) {
[Array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
}
}
for (int i = 0; i < Array.count; i++) {
NSLog(@"姓名 = %@ 学号 = %d 性别 = %@ 年龄= %d 地址 = %@",[Array[i] name],[Array[i] number],[Array[i] sex],[Array[i] age],[Array[i] adress]);
}
NSLog(@"----------------------------------按学号进行排序");
for (int i = 0; i < Array.count - 1; i++) {
for (int j = 0; j < Array.count - 1 - i; j++) {
if ([Array[j] number] > [Array[j + 1] number]) {
[Array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
}
}
for (int i = 0; i < Array.count; i++) {
NSLog(@"姓名 = %@ 学号 = %d 性别 = %@ 年龄= %d 地址 = %@",[Array[i] name],[Array[i] number],[Array[i] sex],[Array[i] age],[Array[i] adress]);
}
NSLog(@"----------------------------------按姓名进行排序");
for (int i = 0; i < Array.count - 1; i++) {
for (int j = 0; j < Array.count - 1 - i; j++) {
if ([[Array[j] name] compare:[Array[j + 1] name]] == NSOrderedDescending) {
[Array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
}
}
for (int i = 0; i < Array.count; i++) {
NSLog(@"姓名 = %@ 学号 = %d 性别 = %@ 年龄= %d 地址 = %@",[Array[i] name],[Array[i] number],[Array[i] sex],[Array[i] age],[Array[i] adress]);
}