我要訂購NSMutableArray
包含對象根據兩個對象屬性其中之一有double
類型第二個有NSString
?按照double值排序NSmutable數組包含對象
我試圖用這個方法,但我得到異常:
NSString * LASTNAME = @"lastName";
NSString * GRADE = @"grade";
NSSortDescriptor *lastDescriptor =
[[NSSortDescriptor alloc]
initWithKey:LASTNAME
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *firstDescriptor =
[[NSSortDescriptor alloc]
initWithKey:GRADE
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
NSArray * descriptors =
[NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil];
NSArray * sortedArray =
[students sortedArrayUsingDescriptors:descriptors];
@interface Student : NSObject
{
NSString * firstName;
NSString * lastName;
double grade;
}
我應該做高檔的NSNumber代替雙我想比較,但我又站了異常? – kartal
@salamonti:鍵值編碼將返回一個雙倍的NSNumber,所以這不成問題。你得到的確切例外是什麼? – Chuck
你是對的,我通過刪除比較解決它,所以它將是 NSSortDescriptor * firstDescriptor = [[NSSortDescriptor alloc] initWithKey:GRADE 遞增:YES]]; – kartal