用另一个NSArray自定义对象对NSArray自定义对象进行排序 [英] Sort NSArray custom objects by another NSArray custom objects

查看:126
本文介绍了用另一个NSArray自定义对象对NSArray自定义对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 2 different NSArray with custom objects as follows,

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数组进行排序.

Now I want to sort my items array based on specList array by comparing "number" property.

现在我的预期项目列表是

Now my expected items list is,

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

我经历了以下几个示例,但是我不知道如何与自定义对象进行比较.任何帮助将不胜感激,在此先感谢.

I have gone through several samples as listed below but I couldn't figure out how to compare with custom objects. Any help would be appreciated, Thanks in advance.

示例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来使日志更清晰:

I override -description to make log clearer:

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

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

In other words:
You have to find the index of the corresponding Item object inside specList (see indexForItem:inList:). I used integerValue because you are using @"002" and @"2", which aren't equal strings.
Then in the NSComparator you compare the two indexes.

对于结尾处的item1item5,我让它们好像.由于它们不在specList中,因此不能保证它们的顺序.如果要按升序"顺序放置它们,则必须执行以下操作:

For item1 and item5 which are at the end, I let them as if. There is no guarantee of their order since they are not present in specList. If you want to put them in "ascending" order, you have to do instead:

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自定义对象对NSArray自定义对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆