緯度、経度、距離を保持する NSDictionary HospitalDictionary を作成しました
NSMutableArray *HospitalArray = [NSMutableArray array];
NSDictionary *HospitalDictionary = [[NSDictionary alloc]init];
NSString *LATITUDE = @"latitude";
NSString *LONGITUDE = @"longitude";
NSString *DISTANCE = @"distance";
for (int i=0; i<=100; i++) {
// calculations for coordinates and distance are done ....
HospitalDictionary = [NSDictionary dictionaryWithObjectsAndKeys:latitude, LATITUDE,
longitude, LONGITUDE,[NSNumber numberWithInt:distanceInKm], DISTANCE, nil];
[HospitalArray addObject:HospitalDictionary];
{
次に、次のコードを使用してデータが短い
// These line are added to short the Dictionary added to Array List with the Key of Distance
NSSortDescriptor *distanceDescriptor = [[NSSortDescriptor alloc] initWithKey:DISTANCE ascending:YES];
id obj;
NSEnumerator * enumerator = [HospitalArray objectEnumerator];
NSArray *descriptors = [NSArray arrayWithObjects:distanceDescriptor, nil];
NSArray *sortedArray = [HospitalArray sortedArrayUsingDescriptors:descriptors];
enumerator = [sortedArray objectEnumerator];
// this will print out the shorted list for DISTANCE
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);
// this will return the object at index 1
NSLog(@"Selected array is %@",[sortedArray objectAtIndex:1]);
これの出力は -
2012-05-30 08:24:42.784 Hospitals[422:15803]
sorted array is
{
distance = 1;
latitude = "27.736221";
longitude = "85.330095";
}
sortedArray の ObjectAtIndex:1 の緯度または経度のみを取得したい。特定の値を取得するにはどうすればよいですか。完全なリストではなく、sortedArray からの緯度。