你如何防止sortedArrayUsingSelector删除重复的条目(ios)?

时间:2021-08-09 07:38:00

How can I use the following method to sort my array and keep the duplicates? All I want is to sort the distance array and have the lineItems array sort in the same order so that my line items are sorted by distance. Is there an easy way to do this? I've tried many different implementations with no luck.

如何使用以下方法对数组进行排序并保留重复项?我想要的是对距离数组进行排序,并使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];
        }

My bubble sort implementation:

我的冒泡排序实现:

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 个解决方案

#1


3  

Your problem isn't -sortedArrayUsingSelector:, it's NSDictionary's -allKeys method. Keys are unique in a dictionary, so the -allKeys array won't have any duplicates.

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

Use a separate NSArray instance to store the result of the sort, or use inafziger's suggestion.

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

#2


1  

Instead of using your sort function, try this:

请尝试以下方法,而不是使用您的排序功能:

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

Then you don't need the dictionary at all.

那你根本不需要字典。

#3


0  

why not just do an old fashion bubble sort?

为什么不只是做一个旧的时尚泡沫排序?

something like this:

像这样的东西:

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

and they're all sorted with duplicates (it's not pretty but it works)

并且他们都是重复排序(它不漂亮,但它的工作原理)

edit:

if nothing works..make a objective c object with something like: coord1, coord2, distance, and maybe an unique id ..so you never have a duplicate item .Declare them as property to that object and then bubble sort them (with maybe changing the id-s too) .Like if you have an object with id=2 and you move it in array on index 1, then change the id to match the index...or something to that effect.Then you wont need the dictionary cuz you have the coord in the object itself...it should be easier to do ..at least in my mind

如果没有任何作用..用一个像coord1,coord2,distance,也许是一个唯一的id来制作一个客观的c对象。所以你永远不会有一个重复的项目。将它们作为该对象的属性,然后对它们进行冒泡排序(可能也改变了id-s)。如果你有一个id = 2的对象并且你在索引1上的数组中移动它,那就改变id以匹配索引......或者那样的东西。然后你不需要字典因为你在对象本身中有coord ...它应该更容易做..至少在我的脑海里

#4


0  

I ended up just turning the lineItems into array of mutable dictionaries and adding the distance into each line item. Then using the sortUsingDescriptor method. I appreciate everyone's help! Something so simple was so difficult for me... thanks:

我最终只是将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]]];

#1


3  

Your problem isn't -sortedArrayUsingSelector:, it's NSDictionary's -allKeys method. Keys are unique in a dictionary, so the -allKeys array won't have any duplicates.

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

Use a separate NSArray instance to store the result of the sort, or use inafziger's suggestion.

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

#2


1  

Instead of using your sort function, try this:

请尝试以下方法,而不是使用您的排序功能:

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

Then you don't need the dictionary at all.

那你根本不需要字典。

#3


0  

why not just do an old fashion bubble sort?

为什么不只是做一个旧的时尚泡沫排序?

something like this:

像这样的东西:

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

and they're all sorted with duplicates (it's not pretty but it works)

并且他们都是重复排序(它不漂亮,但它的工作原理)

edit:

if nothing works..make a objective c object with something like: coord1, coord2, distance, and maybe an unique id ..so you never have a duplicate item .Declare them as property to that object and then bubble sort them (with maybe changing the id-s too) .Like if you have an object with id=2 and you move it in array on index 1, then change the id to match the index...or something to that effect.Then you wont need the dictionary cuz you have the coord in the object itself...it should be easier to do ..at least in my mind

如果没有任何作用..用一个像coord1,coord2,distance,也许是一个唯一的id来制作一个客观的c对象。所以你永远不会有一个重复的项目。将它们作为该对象的属性,然后对它们进行冒泡排序(可能也改变了id-s)。如果你有一个id = 2的对象并且你在索引1上的数组中移动它,那就改变id以匹配索引......或者那样的东西。然后你不需要字典因为你在对象本身中有coord ...它应该更容易做..至少在我的脑海里

#4


0  

I ended up just turning the lineItems into array of mutable dictionaries and adding the distance into each line item. Then using the sortUsingDescriptor method. I appreciate everyone's help! Something so simple was so difficult for me... thanks:

我最终只是将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]]];