【问题标题】:How do you prevent sortedArrayUsingSelector from removing duplicate entries (ios)?如何防止 sortedArrayUsingSelector 删除重复条目(ios)?
【发布时间】:2012-04-12 22:11:19
【问题描述】:

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

【问题讨论】:

  • dictionary 中有什么内容?
  • Inafziger,我添加了更多代码
  • 你指的是什么方法? sortedArrayUsingSelector:?
  • 是的,sortedArrayUsingSelector:@selector(compare:) 在对它们进行排序时取出重复项。这弄乱了我的索引
  • NSLog(@"DICTIONARY BEFORE %@", dictionary); 添加到您的程序并发布结果。

标签: ios arrays sorting duplicates nsdictionary


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    不要使用排序功能,试试这个:

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

    那你根本不需要字典。

    【讨论】:

      【解决方案3】:

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

      类似这样的:

      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 以匹配索引...或类似的效果。然后你不需要字典,因为您在对象本身中有坐标......应该更容易做到......至少在我看来

      【讨论】:

      • 尝试了这种冒泡排序并得到一个无限循环,直到我在交换对象之前删除了它们。但是,这仍然是对数字进行无序排序。我在上面的描述中添加了我的冒泡排序实现。关于出了什么问题的任何线索?
      • 我的目标很简单,我想对距离数组进行排序,并以完全相同的方式对订单项进行排序,以便我的订单项按距离排序。出于某种原因,这对我来说在 ios 中非常困难......
      • 你在用字典做什么?如果我正确理解这一点,您正在尝试保存在键是值而对象是另一个值的字典中。那是错误的!字典会覆盖给定键的先前值...所以要么使用 2 个数组,要么查看 NSSet(虽然 NSSet 是 NSArray..so..yea..same 的子类)
      • 我不确定你的意思,我根据你的冒泡排序把上面的解释说得更清楚了(抱歉,我不小心留在了旧代码中,请再次检查),但仍然有问题. lineItems 是一个字典数组。这些字典中的每一个都有纬度和经度。我使用这些坐标来创建距离数组。我想对距离数组进行排序,但也让 lineItems 数组以相同的顺序与它一起排序。也许有更优雅的方式来做到这一点?
      【解决方案4】:

      我最终只是将 lineItems 转换为可变字典数组,并将距离添加到每个 line item 中。然后使用 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]]];
      

      【讨论】:

      • 此代码泄漏(除非您使用 ARC)。您想在循环结束时显式释放newLineItem。将其设置为nil 只会丢失对-mutableCopy 分配的NSMutableDictionary 的引用。
      猜你喜欢
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      相关资源
      最近更新 更多