//
// main.m
// oc-遍歷和排序-homework
//// created by dllo on 16/2/25.
//#import #import "person.h"
int main(int argc, const char * argv)
nsmutablestring *str = [nsmutablestring stringwithcapacity:20];
for (nsstring *string in strarray)
nslog(@"%@", str);
// 要求:
// 1、建立1個包含5個person物件的陣列(姓名 英 ); 2、 for迴圈找到年齡最 的person物件; 3、 for...in遍歷陣列,把 於某個年齡的person物件的姓名後 拼接上字串「-brother」。
person *per1 = [[person alloc]initwithname:@"ma" ***:@"male" age:26];
person *per2 = [[person alloc]initwithname:@"liutaifeng" ***:@"male" age:22];
person *per3 = [[person alloc]initwithname:@"zhengfeng " ***:@"male" age:23];
person *per4 = [[person alloc]initwithname:@"ma" ***:@"male" age:27];
person *per5 = [[person alloc]initwithname:@"ma" ***:@"male" age:29];
nsarray *array = [nsarray arraywithobjects:per1, per2, per3, per4, per5, nil];
person *per = [[person alloc]initwithname:@"majian" ***:@"male" age:29];
for (int i = 0; i < array.count; i++)
}nslog(@"%@", [per name]);
nsarray *newarray = [array sortedarrayusingselector:@selector(comparewithname:)];
nslog(@"%@", newarray);
nsarray *newarray2 = [array sortedarrayusingselector:@selector(comparewith***:)];
nslog(@"%@", newarray2);
nsarray *newarray3 = [array sortedarrayusingselector:@selector(comparewithage:)];
nslog(@"%@", newarray3);
nssortdescriptor *sortofname = [[nssortdescriptor alloc]initwithkey:@"name" ascending:yes];
nssortdescriptor *sortofage = [[nssortdescriptor alloc]initwithkey:@"age" ascending:yes];
nsarray *array1 = [array sortedarrayusingdescriptors:@[sortofname, sortofage]];
nslog(@"%@", array1);
}return 0;
}
person.m檔案:
////oc-遍歷和排序-homework
////
created by dllo on 16/2/25.
////
#import
"person.h
"@implementation
person
- (instancetype)initwithname:(nsstring *)name ***:(nsstring *)*** age:(nsinteger)age
return
self;
}- (nscomparisonresult)comparewithname:(person *)anotherperson
- (nscomparisonresult)comparewithage:(person *)anotherperson
else
if(self.age else
}- (nscomparisonresult)comparewith***:(person *)anotherperson
- (nsstring *)description
@end
iOS 陣列排序和遍歷
1 擷取陣列 nsarray arr2 arr subarraywithrange nsmakerange 0,4 2 找出索引 nsinteger index arr indexofobject nonnull id 3 讓陣列中每個物件都呼叫某個方法 arr makeobjectsperform...
c Dictionary的遍歷和排序
c 遍歷的兩種方式 for和foreach for 需要指定首位資料 末尾資料 資料長度 for遍歷語句中可以改變資料的值 遍歷規則可以自定義,靈活性較高 foreach 需要實現ienumerator介面 在遍歷中不可以改變資料的值 遍歷規則只能是 但查詢效率較高 dictionary遍歷方式 d...
c Dictionary的遍歷和排序
c 遍歷的兩種方式 for和foreach for 需要指定首位資料 末尾資料 資料長度 for遍歷語句中可以改變資料的值 遍歷規則可以自定義,靈活性較高 foreach 需要實現ienumerator介面 在遍歷中不可以改變資料的值 遍歷規則只能是 但查詢效率較高 dictionary遍歷方式 d...