NSSortDescriptor: Custom comparison on multiple keys simultaneously

不打扰是莪最后的温柔 提交于 2019-12-04 07:06:39
Nathan Villaescusa

You could sort with a block instead:

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

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.

For NSFetchedResultsController you might want to try something else:

It looks like you can pass 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];

APIs for sorting are generally capable to use an array of NSSortDescriptors, not just one, so why not use them?

For example, NSArray has a method called sortedArrayUsingDescriptors: (note the plural form) which takes an array of NSSortDescriptor objects.

So you can simply write this:

NSSortDescriptor *endCalYearSD = [NSSortDescriptor sortDescriptorWithKey:@"endCalYear" ascending:YES];
NSSortDescriptor *endMonthSD = [NSSortDescriptor sortDescriptorWithKey:@"endMonth" ascending:YES];
NSSortDescriptor *periodLenSD = [NSSortDescriptor sortDescriptorWithKey:@"periodLength" ascending:YES];

NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:@[endCalYearSD, endMonthSD, periodLenSD]];

This way, your originalArray will be sorted first by endCalYear, each entry with the same endCalYear will be sorted by endMonth, and each entry with same endCalYear and endMonth will then be sorted by periodLendth.

You have APIs that use an array of sortDescriptors for most of the APIs that propose sorting (including CoreData and such) so the principle is the same all the time.


If you really need to stick with only one NSSortDescriptor (and your sorting algorithm isn't flexible enough to use a block-based comparator or an array of NSSortDescriptors), you can simply provide a property to your custom object that compute some value on which you can base your sorting algorithm.

For example add such method to your custom class:

-(NSUInteger)sortingIndex {
    return endCalYear*10000 + endCalMonth*100 + periodLength;
}

Then sort on this key/property. This is not really very clean to read and a very pretty design pattern, but the better way would be to alter your sorting algorithm API to allow sorting on multiple keys at once, so…


[EDIT] (to answer your [EDIT] on block-based API)

I don't see why the block-based API sortDescriptorWithKey:ascending:comparator: won't work for you. You can specify whatever custom NSComparator block you need there, so this block can tell, given two objects, which one is before the other. The way you determine which one is before which one is up to you, you can compare only endCalYear, or endCalYear and endMonth, etc. so there is no limitation here about sorting using multiple keys.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!