ios - 在多个属性上对 NSArray 自定义对象进行排序

标签 ios objective-c sorting nsarray

我有一个具有多个属性的自定义对象。

一个属性是一个类型,例如一个枚举:A、B、C 等

这些也都有日期。

@property (assign) CustomType customType;
@property (nonatomic, strong) NSDate *startDate;
...

typedef NS_ENUM(NSInteger, CustomType) {
    A,
    B,
    C
};

我想先放置“C”类型的所有对象,然后按日期降序排列其余对象。

实现此目标的最佳方法是什么?

我已经尝试添加多个 sortDescriptors,但我只希望 C 对象位于顶部,然后其余按日期排序,这按日期对每个分组进行排序。

NSSortDescriptor *sortDescriptor1 =[[NSSortDescriptor alloc] initWithKey:@"customType" ascending:NO];
NSSortDescriptor *sortDescriptor2 =[[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
NSArray *sortedObjects = [_items sortedArrayUsingDescriptors:sortDescriptors];

例子

+-------------------------+
| C | 2017-08-16 12:48:53 |
| C | 2017-08-15 12:46:53 |
| B | 2017-08-16 12:48:53 |
| B | 2017-08-14 12:43:53 |
| A | 2017-08-15 12:46:53 |
| A | 2017-08-14 12:31:53 |
+-------------------------+

想要

+-------------------------+
| C | 2017-08-16 12:48:53 |
| C | 2017-08-15 12:46:53 |
| A | 2017-08-16 12:48:53 |
| B | 2017-08-15 12:46:53 |
| A | 2017-08-14 12:43:53 |
| B | 2017-08-14 12:31:53 |
+-------------------------+

我也可以先决定我想要哪个 customType,所以我可能希望 B 位于顶部,然后其余的只是按日期?


将数组分成两组是否更容易,所有 C 然后其余。对每个子组进行排序,然后将它们重新组合在一起?

然后我可以在下面的谓词中选择我想要的顶级组:

NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"customType == %ld", C];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"customType != %ld", C];
NSArray *top = [_items filteredArrayUsingPredicate:pred1];
NSArray *bottom = [_items filteredArrayUsingPredicate:pred2];

NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
NSArray *sortedTop = [top sortedArrayUsingDescriptors:sortDescriptors];
NSArray *sortedBottom = [bottom sortedArrayUsingDescriptors:sortDescriptors];

//NSArray *items = [NSArray arrayWithObjects:sortedTop, sortedBottom, nil];

NSArray *newArray = @[];
newArray = [newArray arrayByAddingObjectsFromArray:sortedTop];
newArray = [newArray arrayByAddingObjectsFromArray:sortedBottom];

_items = [newArray mutableCopy];

按日期排序 https://stackoverflow.com/a/6084944/2895831

NSLog(@"nodeEventArray == %@", nodeEventArray);
NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"startDate" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [nodeEventArray sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"sortedEventArray == %@", sortedEventArray);

过去,我按给定属性对数组进行分组,并对单个属性使用自定义排序。

How to use Custom ordering on NSArray

@property (nonatomic, strong) NSString *level;

NSArray *levels = [array valueForKeyPath:@"@distinctUnionOfObjects.level"];

NSArray *levelsSortOrder = @[@"Beginner", @"Basic", @"Intermediate", @"Expert", @"Advanced", @"Developer", @"Seminar"];

NSArray *sortedArray = [levels sortedArrayUsingComparator: ^(id obj1, id obj2){
    NSUInteger index1 = [levelsSortOrder indexOfObject: obj1];
    NSUInteger index2 = [levelsSortOrder indexOfObject: obj2];
    NSComparisonResult ret = NSOrderedSame;
    if (index1 < index2)
    {
        ret = NSOrderedAscending;
    }
    else if (index1 > index2)
    {
        ret = NSOrderedDescending;
    }
    return ret;
}];

最佳答案

我喜欢sortedArrayUsingComparator:,并且我认为您无法使用其他方法做您想做的事情。

sortedArrayUsingComparator: 提供了有关如何排序的非常大的操作。 其背后的逻辑是: 比较它们之间的两个对象(obj1obj2)。 你决定这两个的排序。 它将迭代整个数组,并通过两两比较每个对象,得到排序结果。 因此,在该 block 中,您必须准确地明确定义为什么一个对象相对于另一个对象“优先”的原因。 这里的CustomType就是其中之一,并且优先于startDate。那么我们就按照这个顺序来做吧。

接下来的实现:

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator: ^(CustomClass *obj1, CustomClass2 *obj2){ 
    CustomType type1 = [obj1 customType]; 
    CustomType type2 = [obj2 customType]; 
    if (type1 == C && type2 == C)
    {
        return [[obj1 startDate] compare:[obj2 startDate]];
    }
    else if (type1 == C && type2 != C) 
    {
        return NSOrderedAscending;
    }
    else if (type1 != C && type2 == C)
    {
        return NSOrderedDescending;
    }
    else //if (type1 != C && type2 != C)
    {
        return [[obj1 startDate] compare:[obj2 startDate]];
    }
}];

进行一些重构:

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator: ^(CustomClass *obj1, CustomClass2 *obj2){ 
    CustomType type1 = [obj1 customType]; 
    CustomType type2 = [obj2 customType]; 
    if (type1 == C && type2 != C) 
    {
        return NSOrderedAscending;
    }
    else if (type1 != C && type2 == C)
    {
        return NSOrderedDescending;
    }
    else //Either (type1 == C && type2 == C) OR (type1 != C && type2 != C)
    {
        //return [[obj1 startDate] compare:[obj2 startDate]];
        return [[obj2 startDate] compare:[obj1 startDate]];
    }
}];

请注意,NSOrderedAscendingNSOrderedDescending 可能必须颠倒过来,我永远不知道该使用哪一个,我总是进行测试。

我将两个 block 中的 id 替换为您的 CustomClass 因为您已经知道您拥有的对象的类。这避免了一行强制转换:CustomType type1 = [(CustomClass *)obj1 customType];

关于ios - 在多个属性上对 NSArray 自定义对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45674976/

相关文章:

ios - 适用于 iOS 6 的 Paypal MECL?

ios - TableViewRow 中的标签被截断(Titanium Studio)

ios - 当 ScrollView subview 不在屏幕上时手势识别器不工作

ios - 自调整单元格使 UITableView 跳跃

objective-c - 从未调用过 CoreAudio AudioQueue 回调函数,没有报错

javascript - 根据数组排序

sorting - 如何根据函数返回的值在dart中对List进行排序

ios - 如何以编程方式为我的 collectionView UITextFields 提供标签?

c - 删除数组复制步骤时合并排序问题

objective-c - 查找某个div的xpath