6

날짜별로 시작 시간을 정렬하려고합니다. 시작 시간은 자정부터 분입니다. 따라서 시작 시간이 < 인 경우 제대로 정렬되지 않습니다.NSSortDescriptor가 정수를 올바르게 정렬하지 않습니다.

- (NSFetchedResultsController *)fetchedResultsController { 

    if (fetchedResultsController != nil) { 
     return fetchedResultsController; 
    } 

    /* 
    Set up the fetched results controller. 
    */ 
    // Create the fetch request for the entity. 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    // Edit the entity name as appropriate. 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Appointments" inManagedObjectContext:[[DataManager sharedInstance] managedObjectContext]]; 
    [fetchRequest setEntity:entity]; 
    [fetchRequest setIncludesPendingChanges:YES]; 

    // Set the batch size to a suitable number. 
    //[fetchRequest setFetchBatchSize:20]; 

    // Sort using the date/then time property. 
    NSSortDescriptor *sortDescriptorDate = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES]; 
    NSSortDescriptor *sortDescriptorTime = [[NSSortDescriptor alloc] initWithKey:@"start_time" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorDate, sortDescriptorTime, nil]; 


    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // Use the sectionIdentifier property to group into sections. 
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[[DataManager sharedInstance] managedObjectContext] sectionNameKeyPath:@"date" cacheName:@"List"]; 
    aFetchedResultsController.delegate = self; 
    self.fetchedResultsController = aFetchedResultsController; 
    NSLog(@"FetchedController: %@", fetchedResultsController); 
    return fetchedResultsController; 
} 

어떻게이 정렬 정수를 올바르게 만들 수 있습니까?

+0

* * 귀하의 start_time (핵심 데이터 개체 모델에서)은 NSNumber 개체이며 문자열이 아닙니다. –

답변

27

start_time이 문자열 인 경우 사전 순으로 정렬됩니다. 즉, aab보다 앞에 있으며 이는 112보다 앞에 있음을 의미합니다.

인간 친화적 인 방식으로 정렬하려면 NSStringlocalizedStandardCompare:을 선택자로 사용하십시오.

[NSSortDescriptor sortDescriptorWithKey:@"start_time" ascending:YES selector:@selector(localizedStandardCompare:)] 
+0

그게 다야! 감사. 내 핵심 데이터는 문자열로 API에서 들어온다. 왜냐하면 문자열이 아니라 숫자로 제공되기 때문이다. – Bot

+2

이것은 굉장합니다. 정확히 내가 무엇을 찾고 있었는지. 문자열 열이이 방식으로 정렬 된 문제가있었습니다. 해결책 위에 위의 1,10,11,2,3,4 ...가 해결되었습니다. – shaikh

+0

NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey : @ "dateTimeInSec"오름차순 : 예 선택기 : @selector (localizedStandardCompare :)]; 내 앱을 추락 시켰어. start_time은 유형 NSString 속성 또는 NSNumber입니까? 오류는 - [__ NSCFNumber localizedStandardCompare :] : 인식 할 수없는 선택기가 userInfo (null)로 인스턴스 0x16dc40c0으로 전송되었습니다. – coolcool1994

관련 문제