1

如何使用以下方法对数组进行排序并保留重复项?我想要的只是对距离数组进行排序,并让 lineItems 数组以相同的顺序排序,以便我的订单项按距离排序。是否有捷径可寻?我已经尝试了许多不同的实现,但没有运气。

lineItems = [[NSMutableArray alloc] initWithArray:(NSMutableArray *)[data objectForKey:@"line_items"]];
distanceArray = [[NSMutableArray alloc] initWithCapacity:[lineItems count]];
        for (int i = 0; i < [lineItems count]; i++) {
            CLLocation *spotLocation = [[CLLocation alloc] initWithLatitude:[[[lineItems objectAtIndex:i] objectForKey:@"latitude"] floatValue] longitude:[[[lineItems objectAtIndex:i] objectForKey:@"longitude"] floatValue]];
            CLLocationDistance distance = ([myLocation distanceFromLocation:spotLocation] / 1000) * 0.621371192;
            NSNumber *foo = [[NSNumber alloc] initWithDouble:distance];
            [distanceArray insertObject:foo atIndex:i];
        }

我的冒泡排序实现:

for (int i=0;i<[distanceArray count]-1;i++){
            for(int j=1;j<[distanceArray count];j++){
                if ([[distanceArray objectAtIndex:i]doubleValue] >[[distanceArray objectAtIndex:j]doubleValue]){
                    NSNumber *temp_i = [[NSNumber alloc] initWithDouble:[[distanceArray objectAtIndex:i]doubleValue]];
                    NSNumber *temp_j = [[NSNumber alloc] initWithDouble:[[distanceArray objectAtIndex:j]doubleValue]];
                    [distanceArray removeObjectAtIndex:i];
                    [distanceArray removeObjectAtIndex:j];
                    [distanceArray insertObject:temp_j atIndex:i];
                    [distanceArray insertObject:temp_i atIndex:j];

                    NSDictionary *tempObj_i = [[NSDictionary alloc] initWithDictionary:[lineItems objectAtIndex:i]];
                    NSDictionary *tempObj_j = [[NSDictionary alloc] initWithDictionary:[lineItems objectAtIndex:j]];
                    [lineItems removeObjectAtIndex:i];
                    [lineItems removeObjectAtIndex:j];
                    [lineItems insertObject:tempObj_j atIndex:i];
                    [lineItems insertObject:tempObj_i atIndex:j];
                }
            }
        }
4

4 回答 4

3

你的问题不是-sortedArrayUsingSelector:NSDictionary-allKeys方法。键在字典中是唯一的,因此-allKeys数组不会有任何重复。

使用单独的NSArray实例来存储排序的结果,或者使用 inafziger 的建议。

于 2012-04-13T00:56:19.627 回答
1

而不是使用你的排序功能,试试这个:

[distanceArray sortArrayUsingSelector:@selector(compare:)];

那么你根本不需要字典。

于 2012-04-13T00:30:46.553 回答
0

为什么不做一个老式的冒泡排序呢?

像这样的东西:

distanceArray=[NSMutableArray arrayWithOjects:[ distanceArray allKeys]];
for (int i=0;i<[distanceArray count]-1;i++){
    for(int j=1;j<[distanceArray count];j++){
        if ([[distanceArray objectAtIndex:i]floatValue] >[[distanceArray objectAtIndex:j]floatValue]){
            id temp=[[distanceArray objectAtIndex:i]floatValue];
            [distanceArray insertObject:[[distanceArray objectAtIndex:j]floatValue] atIndex:i];
            [distanceArray insertObject:temp atIndex:j];
        }
    }
}

并且它们都用重复排序(它不漂亮但它有效)

编辑:

如果没有任何效果..用类似的东西制作一个客观的c对象:coord1,coord2,距离,也许还有一个唯一的id ..所以你永远不会有重复的项目。将它们声明为该对象的属性,然后对它们进行冒泡排序(可能也更改 id-s)。就像您有一个 id=2 的对象并将其移动到索引 1 上的数组中,然后更改 id 以匹配索引...或类似的效果。那么您将不需要字典因为你有对象本身的坐标......它应该更容易做......至少在我看来

于 2012-04-13T00:22:06.310 回答
0

我最终只是将 lineItems 转换为可变字典数组并将距离添加到每个行项中。然后使用 sortUsingDescriptor 方法。我感谢大家的帮助!如此简单的事情对我来说太难了……谢谢:

for (int i = 0; i < [lineItems count]; i++) {
            CLLocation *spotLocation = [[CLLocation alloc] initWithLatitude:[[[lineItems objectAtIndex:i] objectForKey:@"latitude"] floatValue] longitude:[[[lineItems objectAtIndex:i] objectForKey:@"longitude"] floatValue]];
            CLLocationDistance distance = ([myLocation distanceFromLocation:spotLocation] / 1000) * 0.621371192;
            NSNumber *foo = [[NSNumber alloc] initWithDouble:distance];

            NSMutableDictionary *newLineItem = [[lineItems objectAtIndex:i] mutableCopy];
            [newLineItem setObject:foo forKey:@"distance"];
            [lineItems removeObjectAtIndex:i];
            [lineItems insertObject:newLineItem atIndex:i];
            newLineItem = nil;
        }
        [lineItems sortUsingDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES]]];
于 2012-04-13T22:30:36.413 回答