Sorted Array选择最大和最小的对象

时间:2011-10-25 06:13:35

标签: objective-c arrays xcode

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO]; 
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]);

我对数组进行了排序,但无法从数组中选择最大和最小的对象。我怎么能这样做?

NSLog屏幕就像这样; 99, 99, 99, 91, 91, 88, 88, 88, 88, 77, 77, 66, 66, 66,

如何在排序数组中取objectforkey:0

2 个答案:

答案 0 :(得分:1)

您是否尝试过[array objectAtIndex:0][array lastObject]?您应该按索引访问数组的元素,而不是objectForKey:

id first = [array objectAtIndex:0];
id last = [array lastObject];

答案 1 :(得分:1)

NSArray *sortedArray = [ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
NSLog(@"Biggest : %@", [sortedArray objectAtIndex:0]);
NSLog(@"Smallest : %@", [sortedArray lastObject]);