【问题标题】:Sort NSArray custom objects by another NSArray custom objects按另一个 NSArray 自定义对象排序 NSArray 自定义对象
【发布时间】:2016-08-04 13:17:34
【问题描述】:

我有 2 个不同的 NSArray 自定义对象如下,

Item *item1 = [[Items alloc] init];
item1.number = @"1";
item1.serailNumber = @"S01";

Item *item2 = [[Items alloc] init];
item2.number = @"2";
item2.serailNumber = @"S02";

Item *item3 = [[Items alloc] init];
item3.number = @"3";
item3.serailNumber = @"S03";

Item *item4 = [[Items alloc] init];
item4.number = @"4";
item4.serailNumber = @"S04";

Item *item5 = [[Items alloc] init];
item5.number = @"5";
item5.serailNumber = @"S05";

NSArray *items = @[item1, item2, item3, item4, item5]; 

NSArray *specList = @[@{"number" : @"002", @"serialNumber" : @"S02"},
                     @{"number" : @"004", @"serialNumber" : @"S04"},
                     @{"number" : @"003", @"serialNumber" : @"S03"}];

现在我想通过比较“number”属性基于specList 数组对我的items 数组进行排序。

现在我的预期项目列表是,

@[item2, item4, item3, item1, item5]

我已经浏览了下面列出的几个示例,但我不知道如何与自定义对象进行比较。任何帮助将不胜感激,在此先感谢。

Sample 1 Sample 2

【问题讨论】:

    标签: ios objective-c sorting nsarray


    【解决方案1】:

    这应该可以解决问题:

    NSArray *sorted = [items sortedArrayUsingComparator:^NSComparisonResult(Item *item1, Item *item2) {
        NSInteger indexForItemEquivalent1InSpecList = [self indexForItem:item1 inList:specList];
        NSInteger indexForItemEquivalent2InSpecList = [self indexForItem:item2 inList:specList];
        return [@(indexForItemEquivalent1InSpecList) compare:@(indexForItemEquivalent2InSpecList)];
    }];
    
    NSLog(@"Sorted: %@", sorted);
    

    与:

    -(NSInteger)indexForItem:(Item *)item inList:(NSArray *)list
    {
        for (NSInteger i = 0; i < [list count]; i++)
        {
            if ([list[i][@"number"] integerValue] == [[item number] integerValue])
            {
                return i;
            }
        }
        return NSIntegerMax; //If not found, we put it at the end of the list
    }
    

    输出:

    Sorted: (
        "<Item 0x146678f0> number: 2 serial: S02",
        "<Item 0x14667e10> number: 4 serial: S04",
        "<Item 0x14667900> number: 3 serial: S03",
        "<Item 0x14654200> number: 1 serial: S01",
        "<Item 0x14667e20> number: 5 serial: S05"
    )
    

    我覆盖-description 以使日志更清晰:

    -(NSString *)description
    {
        return [NSString stringWithFormat:@"<%@ %p> number: %@ serial: %@", [self class], self, _number, _serailNumber];
    }
    

    换句话说:
    您必须在specList 中找到对应的Item 对象的索引(参见indexForItem:inList:)。我使用了integerValue,因为您使用的是@"002" 和@"2",它们不是相等的字符串。
    然后在NSComparator 中比较两个索引。

    对于最后的item1item5,我让他们好像。由于它们不在specList 中,因此无法保证它们的顺序。如果你想把它们按“升序”排列,你必须这样做:

    NSInteger indexForItem1InSpecList = [self indexForItem:item1 inList:specList];
    NSInteger indexForItem2InSpecList = [self indexForItem:item2 inList:specList];
    if (indexForItem1InSpecList == NSIntegerMax && indexForItem2InSpecList == NSIntegerMax)
    {
        return [@([[item1 number] integerValue]) compare:@([[item2 number] integerValue])];
    }
    else
    {
        return [@(indexForItem1InSpecList) compare:@(indexForItem2InSpecList)];
    }
    

    【讨论】:

      【解决方案2】:

      下面是一个引用第一个数组对第二个数组进行排序的示例:

      NSArray *users = @[@"Dave",@"Mike",@"Joe",@"Jason",@"Kevin"];
      NSArray *iqs = @[@110,@145,@75,@122,@130];
      
      NSMutableArray *array = [NSMutableArray array];
      for (int idx = 0;idx<[users count];idx++) {
          NSDictionary *dict = @{@"Name": users[idx],@"IQ":iqs[idx]};
          [array addObject:dict];
      }
      
      NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"IQ" ascending:NO];
      [array sortUsingDescriptors:@[descriptor]];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多