Objective c NSMUTABLEARRY排序返回的顺序不正确
我试图根据两个位置(当前位置和第一个for循环中指定的坐标)之间的距离对NSMutableArray进行排序。但是,排序并没有返回任何特定的顺序,而是一个完全随机的顺序(请参见此处的图片:) 你能告诉我哪里出了问题吗 分类:Objective c NSMUTABLEARRY排序返回的顺序不正确,objective-c,ios,sorting,comparison,nsmutablearray,Objective C,Ios,Sorting,Comparison,Nsmutablearray,我试图根据两个位置(当前位置和第一个for循环中指定的坐标)之间的距离对NSMutableArray进行排序。但是,排序并没有返回任何特定的顺序,而是一个完全随机的顺序(请参见此处的图片:) 你能告诉我哪里出了问题吗 分类: [self.venues sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [[obj1 objectForKey:@"distance"] compare:[obj2 objec
[self.venues sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj1 objectForKey:@"distance"] compare:[obj2 objectForKey:@"distance"]];
}];
-设置场地:
- (void) setVenues:(NSMutableArray *)_venues {
if (venues != _venues) {
[venues release];
...
NSMutableArray *updatedVenues = [NSMutableArray array];
for (NSDictionary *venue in _venues) {
...
if (address != nil && city != nil) {
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[_location objectForKey:@"lat"] floatValue], [[_location objectForKey:@"lng"] floatValue]);
CLLocation *currentLoc = [[CLLocation alloc] initWithLatitude:self.currentLocation.latitude longitude:self.currentLocation.longitude];
CLLocation *venueLoc = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
CLLocationDistance distance = [venueLoc distanceFromLocation:currentLoc];
float miles = distance / 1000;
miles *= 0.621371192; // metres to miles
NSMutableDictionary *newLocation = [NSMutableDictionary dictionaryWithDictionary:_location];
[newLocation removeObjectForKey:@"distance"];
[newLocation setObject:[NSNumber numberWithFloat:miles] forKey:@"distance"];
_location = [NSDictionary dictionaryWithDictionary:newLocation];
...
NSMutableDictionary *newVenue = [NSMutableDictionary dictionaryWithDictionary:venue];
[newVenue removeObjectForKey:@"location"];
[newVenue setObject:_location forKey:@"location"];
[updatedVenues addObject:newVenue];
}
}
venues = [updatedVenues retain];
}
}
看起来您需要按地点、位置、距离进行排序:
[self.venues sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSNumber *distance1 = [[obj1 objectForKey:@"location"] objectForKey:@"distance"];
NSNumber *distance2 = [[obj2 objectForKey:@"location"] objectForKey:@"distance"];
return [distance1 compare:distance2];
}];
如果您使用的是Xcode>=4.4(iOS为4.5)版本,您可以执行以下操作:
[self.venues sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1[@"location"][@"distance"] compare:obj2[@"location"][@"distance"]];
}];
看起来您需要按地点、位置、距离进行排序:
[self.venues sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSNumber *distance1 = [[obj1 objectForKey:@"location"] objectForKey:@"distance"];
NSNumber *distance2 = [[obj2 objectForKey:@"location"] objectForKey:@"distance"];
return [distance1 compare:distance2];
}];
如果您使用的是Xcode>=4.4(iOS为4.5)版本,您可以执行以下操作:
[self.venues sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1[@"location"][@"distance"] compare:obj2[@"location"][@"distance"]];
}];
您可以将
CLLocation*currentLoc=..
移动到循环之外。在发布之前,您已经这样做了,只是没有更新代码!:)顺便说一句,您可以使用以Xcode 4.4开头的字典[@“key”]语法。您可以将CLLocation*currentLoc=..
移动到循环之外。在发布之前是否这样做过,只是没有更新代码!:)顺便说一句,您可以使用从Xcode 4.4开始的字典[@“key”]语法。