从数组中删除重复项,比较其对象的属性 [英] Remove duplicates from array comparing the properties of its objects

查看:209
本文介绍了从数组中删除重复项,比较其对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类Event,它有两个属性:action(NSString)和date(NSDate)。



假设我有一个Event对象数组。问题是日期属性可以匹配。



我需要删除重复的内容,这意味着相同日期的2个不同对象是重复的。



我可以删除任何字符串或nsdates数组中的重复项,它们很容易比较。但是,如何用复杂的对象来对待他们的属性呢?



不要问我到目前为止我做了什么,我的想法是一个泡沫排序,但这是一个新手的解决方案,而且



非常感谢任何帮助(链接,提示,代码)。



提前感谢。



编辑



感谢dasblinkenlight,我做了一个自定义的方法: / p>

   - (NSArray *)removeDuplicatesInArray:(NSArray *)arrayToFilter {

NSMutableSet * seenDates = [NSMutableSet组];
NSPredicate * dupDatesPred = [NSPredicate predicateWithBlock:^ BOOL(id obj,NSDictionary * bind){
YourClass * e =(YourClass *)obj;
BOOL seen = [seenDates containsObject:e.propertyName];
if(!seen){
[seenDates addObject:e.when];
}
return!seen;
}];
return [arrayToFilter filteredArrayUsingPredicate:dupDatesPred];
}

这里 YourClass 是对象所属类的名称, propertyName 是您要比较的对象的属性



假设self.arrayWithObjects包含YourClass的对象。



填充后,使用



self.arrayWithObjects = [self removeDuplicatesInArray:self.arrayWithObjects];



你完成了



所有积分到dasblinkenlight。你可以创建一个 NSMutableSet 日期,迭代您的事件列表,只添加您之前未遇到的日期的事件。

  NSMutableSet * seenDates = [ NSMutableSet set]; 
NSPredicate * dupDatesPred = [NSPredicate predicateWithBlock:^ BOOL(id obj,NSDictionary * bind){
Event * e =(Event *)obj;
BOOL seen = [seenDates containsObject:e.date];
if(!seen){
[seenDates addObject:e.date];
}
return!seen;
}];
NSArray * events = ... //这是需要过滤的数组
NSArray * filtered = [events filteredArrayUsingPredicate:dupDatesPred];


Suppose I have a class Event, and it has 2 properties: action (NSString) and date (NSDate).

And suppose I have an array of Event objects. The problem is that "date" properties can match.

I need to remove the duplicates, meaning that 2 different objects with the same date IS a duplicate.

I can remove duplicates in any array of strings or nsdates, they are easy to compare. But how to do it with complex objects, where their properties are to be compared?

Don't ask me what I did so far, cos' the only thing coming in my mind is a bubble sort, but it's a newbie solution, and slow.

Quite any help is highly appreciated (links, tuts, code).

Thanks in advance.

EDIT

Thanks to dasblinkenlight, I have made a custom method:

- (NSArray *)removeDuplicatesInArray:(NSArray*)arrayToFilter{

    NSMutableSet *seenDates = [NSMutableSet set];
    NSPredicate *dupDatesPred = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind) {
        YourClass *e = (YourClass*)obj;
        BOOL seen = [seenDates containsObject:e.propertyName];
        if (!seen) {
            [seenDates addObject:e.when];
        }
        return !seen;
    }];
    return [arrayToFilter filteredArrayUsingPredicate:dupDatesPred];
} 

Here YourClass is the name of your class the object belongs to, and propertyName is the property of that object you are going to compare.

Suppose self.arrayWithObjects contains the objects of YourClass.

After populating it, use

self.arrayWithObjects = [self removeDuplicatesInArray:self.arrayWithObjects];

and you are done.

All credits to dasblinkenlight. Cheers!

解决方案

You can create an NSMutableSet of dates, iterate your event list, and add only events the date for which you have not encountered before.

NSMutableSet *seenDates = [NSMutableSet set];
NSPredicate *dupDatesPred = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind) {
    Event *e = (Event*)obj;
    BOOL seen = [seenDates containsObject:e.date];
    if (!seen) {
        [seenDates addObject:e.date];
    }
    return !seen;
}];
NSArray *events = ... // This is your array which needs to be filtered
NSArray *filtered = [events filteredArrayUsingPredicate:dupDatesPred];

这篇关于从数组中删除重复项,比较其对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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