1

私はここで立ち往生しています......私は2つの配列Arr_titleとArr_distanceを持っています私はこのメソッドを使用してソートされたArr_distanceを取得します

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]autorelease];

sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
                             [NSArray arrayWithObject:sortDescriptor]];

しかし、問題は、sortedFloats配列のインデックスに従ってArr_titleをソートしたいということです...

事前にt​​hxを助けてください..........

4

4 に答える 4

1

ねえ、2つの異なる配列を取得する代わりに、キー title,distanceを使用して1つの辞書を取得し、その辞書を配列に保存してから、その配列を並べ替えます。

于 2012-12-07T05:54:28.490 に答える
1

これを試して、

NSDictionary *dict = [NSDictionary dictionaryWithObjects:Arr_title forKeys:Arr_distance];

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES] autorelease];
sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
                         [NSArray arrayWithObject:sortDescriptor]];


NSMutableArray *sortedTitles = [NSMutableArray array];

for (NSString *key in sortedFloats) {//or just use sortedTitles = [dict objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];
    [sortedTitles addObject:[dict valueForKey:key]]; 
}

NSLog(@"%@",sortedValues);

sortedTitlesと同じ順序で並べ替えられた配列が表示されsortedFloatsます。

于 2012-12-07T06:16:24.857 に答える
0

私があなたの質問を正しく理解していれば、同じタスクにNSDictionaryを使用できます

NSMutableArray *array = [NSMutableArray arrayWithArray:@[ @{@"title" : @"City1",@"dist" : @5},

                             @ {@"title" : @"City2",@"dist" : @2.3 },
                             @{@"title" : @"City3",@"dist" : @11.0 },
                             @{@"title" : @"City4",@"dist" : @1.0},
                             @{@"title" : @"City5",@"dist" : @.5},
                             @{@"title" : @"City6",@"dist" : @13 }]];

NSLog(@"dict<%@>",array);

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"dist" ascending:YES];

NSArray *sortarray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

NSLog(@"sort array = <%@>",sortarray);
于 2012-12-07T06:56:32.160 に答える
0

私は解決策を得ました次のコードを参照してください........。

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]autorelease];

sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
                             [NSArray arrayWithObject:sortDescriptor]];

 NSLog(@" Sorted Array : %@", sortedFloats);

 NSDictionary *dictTitle = [NSDictionary dictionaryWithObjects:Arr_title   forKeys:appDel.Arr_distance];

// Sort the second array based on the sorted first array
sortedTitle = [dictTitle objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];


NSLog(@" Sorted Title : %@",sortedTitle);


// Put the two arrays into a dictionary as keys and values
NSDictionary *dictAddress = [NSDictionary dictionaryWithObjects:Arr_address   forKeys:appDel.Arr_distance];

// Sort the second array based on the sorted first array
sortedAddress = [dictAddress objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];


NSLog(@" Sorted Address : %@",sortedAddress);

別の方法で使用する場合は、配列を保持することを忘れないでください...。

于 2012-12-07T07:44:52.893 に答える