繁体   English   中英

用另一个NSArray自定义对象对NSArray自定义对象进行排序

[英]Sort NSArray custom objects by another NSArray custom objects

我有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]

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

样品1 样品2

这应该可以解决问题:

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

这是参照第一个数组对第二个数组进行排序的示例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM