Iphone 使用数组索引对数组排序
我被困在这里。。。。。。 我有两个数组Arr_title和Arr_distance 我用这个方法得到排序的Arr_距离Iphone 使用数组索引对数组排序,iphone,ios,ios4,sorting,Iphone,Ios,Ios4,Sorting,我被困在这里。。。。。。 我有两个数组Arr_title和Arr_distance 我用这个方法得到排序的Arr_距离 NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]autorelease]; sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]autorelease];
sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
[NSArray arrayWithObject:sortDescriptor]];
但问题是我想根据sortedFloats数组的索引对Arr_标题进行排序
thx提前为我提供帮助 嘿,不要使用两个不同的数组,而是使用键
title,distance
将该字典保存在数组中,然后对该数组进行排序,这样您就可以将组合放在一起,这样就不会因任何排序(按标题和按距离)的映射而出现任何问题 试试这个
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
如果我没弄错你的问题,你可以使用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);
NSMUTABLEARRY*array=[NSMUTABLEARRY array WITHARRAY:@[@{@“title”:@“City1”,“dist”:@5},
@{"标题:"城市2","地区":"2.3",,
@{“标题”:“城市3号”,“地区”:@11.0},
@{标题:@“城市4”,地区:@1.0},
@{"标题:"城市5","地区5",,
@{"标题:"城市6,"地区:"13}]];;
NSLog(@“dict”,数组);
NSSortDescriptor*排序=[[NSSortDescriptor alloc]initWithKey:@“dist”升序:是];
NSArray*sortarray=[array-sortedarray-usingdescriptor:[NSArray-arrayWithObject:sort]];
NSLog(@“sort array=,sortarray);
我得到了解决方案,请参见下面的代码
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);
如果在另一种方法中使用,请不要忘记保留数组……SortedFloat的含义是什么