在iPhone SDK中sorting数组

在我的应用程序中,我有两个数组名listofeventsarr_Distance

Listofevents如下所示:

 ( { Latitude = "34.1356031"; Longitude = "-118.0312634"; VenueName = "Fantasia Billiard"; }, { Latitude = "34.1356031"; Longitude = "-118.0312634"; VenueName = "Dave & Busters"; } ) 

arr_distace的样子

( 54985.5455,54985.5455 )

我已经根据JSON响应中给出的纬度和经度计算距离。 在这里,我想按距离递增的顺序显示列表,所以我按照以下代码sorting了两个数组

对于sorting

 NSDictionary *dict = [NSDictionary dictionaryWithObjects:listOfEvents forKeys:arr_distance]; sortedArray = [arr_distance sortedArrayUsingComparator:^(id firstObject, id secondObject) { return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch]; }]; sortedValues = [NSMutableArray array]; for (NSString *key in sortedArray) { [sortedValues addObject:[dict valueForKey:key]]; } 

问题

但我的问题是,当两个距离相同和场地名称不同,那么它显示我只有第一场地名称两次。

我没有find我的问题。 帮我解决这个问题

你可以尝试下面的代码。 我稍微修改了我以前的解决scheme,以避免使用字典。 字典的早期build议纯粹是基于假设,即关键是独特的,现在看起来不是这样。 所以你可以试试这个:

 NSArray *a = @[@"353.90", @"354.68", @"354.68", @"1.18"]; NSArray *b = @[@{ @"Contestant1":@"Jon jones1", @"Contestant2":@"Anderson silva1"}, @{ @"Contestant1":@"Jon jones2", @"Contestant2":@"Anderson silva2"}, @{ @"Contestant1":@"Jon jones3", @"Contestant2":@"Anderson silva3"}, @{ @"Contestant1":@"Jon jones4", @"Contestant2":@"Anderson silva4"}]; NSArray *sortedArray = [a sortedArrayUsingComparator:^(id firstObject, id secondObject) { return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch]; }]; NSArray *sortedValues = [b sortedArrayUsingComparator:^(id firstObject, id secondObject) { id obj1 = [a objectAtIndex:[b indexOfObject:firstObject]]; id obj2 = [a objectAtIndex:[b indexOfObject:secondObject]]; return [((NSString *)obj1) compare:((NSString *)obj2) options:NSNumericSearch]; }]; NSLog(@"%@",sortedArray); NSLog(@"%@",sortedValues); 

您将距离用作键,但是每个键都必须是唯一的。 由于距离相同,后面的对象将覆盖第一个。

我最好的解决办法是创build一个自定义的类,保存每个条目,你也可以添加一个属性的distance ,并按它sorting。

 @interface Entry : NSObject @property (strong) NSString *longitude @property (strong) NSString *latitude @property (strong) NSString *name @property double distance; @end 

 NSMutableArray *listOfEntries = [NSMutableArray array]; [listofevents enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) { Entry *entry = [[Entry alloc] init]; entry.latitude = [dict objectForKey:@"Latitude"]; entry.longitude = [dict objectForKey:@"Longitude"]; entry.name = [dict objectForKey:@"VenueName"]; entry.distance = DistanceBetweenPoints(currentLocation, CGPointMake(entry.latitude,entry.longitude)); [listOfEntries addObject:entry]; }]; [listOfEntries sortUsingComparator: ^NSComparisonResult(Entry *obj1, Entry *obj2){ return obj1.distance > obj2.distance; } ]; 

另一种select是将名称作为字典中的关键字。

问题很简单。 在字典中,每个键应该是唯一的。 在你的情况下,它不是。

您的密钥: 54985.5455和54985.5455 ,两者相同。 这就是问题出现的原因。 改变其中一个,它会像魅力一样工作。

您可以根据您的结果执行search。 我发现从苹果以下

 -(NSArray*)SortingArray:(NSMutableArray*)unSortedArray { static NSStringCompareOptions compareOption=NSCaseInsensitiveSearch |NSNumericSearch|NSWidthInsensitiveSearch|NSForcedOrderingSearch; NSLocale *locale=[NSLocale currentLocale]; NSComparator comparatorBlock=^(id string1, id string2) { NSRange string1Range=NSMakeRange(0,[string1 length]); return [string1 compare:string2 options:compareOption range:string1Range locale:locale]; }; NSArray *sortedArray=[unSortedArray sortedArrayUsingComparator:comparatorBlock ]; return sortedArray; } 

你可以使用你的string比较选项。 我发现有更多选项来限制search

 NSCaseInsensitiveSearch = 1, NSLiteralSearch = 2, /* Exact character-by-character equivalence */ NSBackwardsSearch = 4, /* Search from end of source string */ NSAnchoredSearch = 8, /* Search is limited to start (or end, if NSBackwardsSearch) of source string */ NSNumericSearch = 64, /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find */ NSDiacriticInsensitiveSearch /* If specified, ignores diacritics (o-umlaut == o) */ NSWidthInsensitiveSearch /* If specified, ignores width differences ('a' == UFF41) */ NSForcedOrderingSearch /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (eg "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */ NSRegularExpressionSearch /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */