NSSortDescriptor:对多个键同时进行自定义比较 [英] NSSortDescriptor: Custom comparison on multiple keys simultaneously

查看:91
本文介绍了NSSortDescriptor:对多个键同时进行自定义比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义对象,其中包含基于时间段的信息,例如属性endCalYear,endMonth和periodLength,表示每个期间的结束及其长度。

I have an custom object which contains time period based information, for instance the attributes endCalYear, endMonth and periodLength which indicate the end of each period and its length.

我想创建一个 NSSortDescriptor 基于或其他排序方法,它结合这三个属性,并允许在所有三个键同时排序此对象。

I would like to create an NSSortDescriptor based or other sorting method which combines these three attributes and allows sorting this object on all three keys at the same time.

例如:

EndCalYear  endMonth  periodLength (months) sortOrder
2012        6         6                     1
2012        6         3                     2
2012        3         3                     3
2011        12        12                    4

排序算法将根据我自己的算法是完全任意的。

The sort algorithm would be completely discretionary based on my own algorithm.

我如何编码这样的算法?

How could I code such an algorithm?

基于块的 sortDescriptorWithKey:ascending:comparator:方法不会在我的视图中工作,只指定一个排序键。

The block based sortDescriptorWithKey:ascending:comparator: method won't work in my view because it would allow me to specify only one sorting key. However, I need to sort on all three keys at the same time.

有关如何解决这个问题的任何想法或想法?

Any ideas or thoughts on how to solve this?

谢谢!

推荐答案

您可以用块代替:

NSArray *sortedArray;
sortedArray = [myArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    MyObject *first = (MyObject*)a;
    MyObject *second = (MyObject*)b;

    if (first.endCalYear < second.endCalYear) {
        return NSOrderedAscending;
    }
    else if (first.endCalYear > second.endCalYear) {
        return NSOrderedDescending;
    }
    // endCalYear is the same

    if (first.endMonth < second.endMonth) {
        return NSOrderedAscending;
    }
    else if (first.endMonth > second.endMonth) {
        return NSOrderedDescending;
    }    
    // endMonth is the same

    if (first.periodLength < second.periodLength) {
        return NSOrderedAscending;
    }
    else if (first.periodLength > second.periodLength) {
        return NSOrderedDescending;
    }
    // periodLength is the same

    return NSOrderedSame;
}]

这个排序方式为 endCalYear ascending,then endMonth 上升,最后 periodLength 升序。

This sorts by endCalYear ascending, then endMonth ascending and finally periodLength ascending. You could modify it to change the order or switch the signs in the if statement to make it descending.

对于NSFetchedResultsController,你可能想要尝试其他的东西:

For NSFetchedResultsController you might want to try something else:

您似乎可以覆盖一个描述符列表,每个列需要排序:

It looks like you can past it a list of descriptors, one for each column that you want sorted:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSSortDescriptor *descriptor1 = [[NSSortDescriptor alloc] initWithKey:@"endCalYear" ascending:YES];
NSSortDescriptor *descriptor2 = [[NSSortDescriptor alloc] initWithKey:@"endMonth" ascending:YES];
NSSortDescriptor *descriptor3 = [[NSSortDescriptor alloc] initWithKey:@"periodLength" ascending:YES];
NSArray *sortDescriptors = @[descriptor1, descriptor2, descriptor3];
[fetchRequest setSortDescriptors:sortDescriptors];

这篇关于NSSortDescriptor:对多个键同时进行自定义比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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