objective-c - NSMutableArray 排序返回不正确的顺序

标签 objective-c ios sorting comparison nsmutablearray

我试图根据两个位置之间的距离(当前位置和第一个 for 循环中指定的坐标)对 NSMutableArray 进行排序。但是,排序不会返回任何特定顺序,而是返回一个完全随机的顺序(参见此处的图片:http://u.maxk.me/iz7Z)。

你能告诉我哪里错了吗?

排序:

[self.venues sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    return [[obj1 objectForKey:@"distance"] compare:[obj2 objectForKey:@"distance"]];

}];

-setVenues:

- (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];

    }

}

最佳答案

看起来你需要按 venue.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"]];

}];

关于objective-c - NSMutableArray 排序返回不正确的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12788705/

相关文章:

java - MapReduce 排序程序中的 NullPointerException

c++ - 从文本文件中排序数据

iphone - 将 UINavigationController 添加到 iPhone 上的 subview

ios - Xcode : Disable rotation for a view in the ViewController. swift

java - Objective C JAVA字节序

ios - 静态成员不能用于协议(protocol)元类型

iOS 确定 NSMutableArray sortUsingComparator 何时完成

java - 如何在java中对嵌套列表进行排序

objective-c - 在 swift assert(0) 中无法将 Int 类型的值转换为预期的参数类型 Bool

ios - 如何在某些情况下停止 iPad 旋转?