2014-10-01 416 views
-5

如何從NSMutableArray獲取最大值。值是作爲存儲在NSString的陣列如下面從NSMutableArray獲取最大值

for(int counter=0; counter<[datesArray count];counter++) 
{ 
    glass = [NSString stringWithFormat:@"%f",[[dbhandler todayData:currentDate] floatValue]]; 
    [graphGlassesArray addObject:glass]; 
} 

陣列名稱是graphGlassesArray

+0

您是否嘗試循環並將迄今爲止遇到的最大值存儲在變量中? – APerson 2014-10-01 12:56:16

回答

4

嘗試可以使用從陣列獲取最大價值:通過排序可變數組

NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:nil ascending:NO selector:@selector(localizedCompare:)]; 
NSString *maxValue = [[graphGlassesArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]] objectAtIndex:0]; 
[desc release]; 
+0

你只比我快了一秒......我並沒有發佈這個想法。 – holex 2014-10-01 13:02:31

0
float max = 0.f; 
for(int counter=0; counter<[datesArray count];counter++) 
{ 
    glass = [NSString stringWithFormat:@"%f",[[dbhandler todayData:currentDate] floatValue]]; 
    [graphGlassesArray addObject:glass]; 
    if(max < [[dbhandler todayData:currentDate] floatValue]) { 
     max = [[dbhandler todayData:currentDate] floatValue]; 
    } 
} 

與此

1

你也應該儘量

NSNumber* max = [graphGlassesArray valueForKeyPath:@"max.self"]; 
float maxFloat = [max floatValue]