6

Tôi đang cố sắp xếp theo ngày rồi bắt đầu thời gian. Thời gian bắt đầu là phút từ nửa đêm. Vì vậy, nếu thời gian bắt đầu là < 100, nó sẽ không sắp xếp đúng cách.NSSortDescriptor không sắp xếp các số nguyên chính xác

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

Tôi làm cách nào để sắp xếp số liệu này đúng?

+0

I * hope * your start_time (trong mô hình đối tượng Dữ liệu cốt lõi) là đối tượng NSNumber chứ không phải chuỗi. –

Trả lời

27

Nếu start_time là một chuỗi thì nó sẽ được sắp xếp theo thứ tự bảng chữ cái có nghĩa là aa là trước b cũng có nghĩa là 11 trước 2.

Để sắp xếp theo cách thân thiện hơn với con người, hãy sử dụng localizedStandardCompare: làm công cụ chọn.

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

Đó là nó! cảm ơn. Tôi có nó trong dữ liệu cốt lõi của tôi như là một chuỗi bởi vì nó đến từ API như một chuỗi và không phải là một số. – Bot

+2

điều này thật tuyệt vời. Chính xác những gì tôi đang tìm kiếm. Tôi đã có vấn đề cột chuỗi của tôi đã được sắp xếp theo cách này 1,10,11,2,3,4 ... giải pháp trên giải quyết nó. – shaikh

+0

NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey: @ "dateTimeInSec" tăng dần: YES selector: @selector (localizedStandardCompare :)]; đã làm hỏng ứng dụng của tôi. Thuộc tính start_time có thuộc tính NSString hay NSNumber không? Lỗi là - [__ NSCFNumber localizedStandardCompare:]: bộ chọn không được nhận dạng được gửi tới instance 0x16dc40c0 với userInfo (null) – coolcool1994

Các vấn đề liên quan