網頁

2014年3月19日 星期三

【iOS】array 排序

【環境:ios7】

日期、字串排序,可在array中放入各種參數比較,相關參閱說明檔
NSSortDescriptor Class Reference
 
[array sortedArrayUsingSelector:@selector(compare:)] //升冪排序

 
//降冪
NSSortDescriptor *SortDescriptor=[NSSortDescriptor sortDescriptorWithKey:Nil ascending:NO selector:@selector(compare:)];
[Array sortedArrayUsingDescriptors:[NSArray arrayWithObject:SortDescriptor]];

 
//自定
NSSortDescriptor *sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@"排序名稱" ascending:isAscending];
NSArray *sortDescriptors=[NSArray arrayWithObject:sortDescriptor];
NSArray *Array=[arr sortedArrayUsingDescriptors:sortDescriptors]; 

 
//block
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSDate *first = [(Person*)a birthDate];
    NSDate *second = [(Person*)b birthDate];
    return [first compare:second];
}];


As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

沒有留言:

張貼留言