使用排序描述符数组获取会不按顺序返回字符串,如何正确排序?

时间:2013-07-23 11:09:17

标签: core-data nssortdescriptor

我正在尝试按顺序返回包含数字的字符串,但有一些地方我的当前排序描述符失败(请参阅下面的代码)。我试图按天数,周数和类别返回我的数据。这是一个例子:

Day 1, week 1
Day 2, week 1

目前,排序在第2周出现故障:

Day 10, week 2
Day 11, week 2
Day 8, week 2
Day 9 week 2

我尝试将localizedCompare与我的最后一个排序描述符一起使用,这没有任何帮助。在此先感谢您的帮助!

- (NSFetchedResultsController *)fetchedResultsController {
    if (!fetchedResultsController ) {
        NSFetchRequest *request = [[NSFetchRequest alloc] init];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Workouts" inManagedObjectContext:[Utils appDelegate].managedObjectContext];
        [request setEntity:entity];

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"w_category" ascending:YES];
        NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"weekNo"
                                                                        ascending:YES];
        Workouts *changeSort;
        NSString *sortingChoice;

        if ([changeSort.w_scoring isEqualToString:@"scoring"]){
            sortingChoice = @"w_programDay";
        } else if (![changeSort.w_scoring isEqualToString:@"scoring"]){
            sortingChoice = @"w_name";
        }

        NSSortDescriptor *sortDescriptor3 =[[NSSortDescriptor alloc] initWithKey:[NSString stringWithFormat:@"%@", sortingChoice] ascending: YES selector: @selector(localizedCompare:)];

        NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, sortDescriptor2,sortDescriptor3, nil];
        [request setSortDescriptors:sortDescriptors];

        NSFetchedResultsController *newController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:@"w_category" cacheName:nil];

        newController.delegate = self;
        self.fetchedResultsController = newController;
    }

    return fetchedResultsController;
}

0 个答案:

没有答案