カテゴリと金額の値をコア データに保存しています。上位 5 つのカテゴリをユーザーに表示したいです。結果を取得することはできますが、同様のカテゴリの金額を合計して上位 5 を表示するにはどうすればよいですか?
カテゴリの金額を合計するためのロジックをいくつか試しましたが、そこで立ち往生しており、それが正しいかどうかも知りたいです。
NSArray *results = [moc executeFetchRequest:request error:&error];
for(int i = 0; i < [results count] ; i ++){
double sum = 0;
NSString *temp = [[results objectAtIndex:i]valueForKey:@"category"];
for( int j = 0; j< [results count] ; j ++){
if([temp isEqualToString:[[results objectAtIndex:j]valueForKey:@"category"]]){
sum = sum + [[[results objectAtIndex:j]valueForKey:@"amount"]doubleValue];
}
}
NSLog(@"%f",sum);
NSLog(@"%f",temp);
}
}