簡體   English   中英

從核心數據獲取時,如何根據ios中的距離和名稱排序

[英]How can we sort based on distance and name in ios when fetching from core data

我正在使用Restkit從數據庫中檢索數據數組。 我希望根據參數距離的升序對數組進行排序。 問題是,如果數組包含兩個或兩個以上具有相同距離的條目,則必須根據其名稱的升序依次對其進行排序。 我目前正在使用NSSortDescriptor

RKManagedObjectStore *managedObjectStore      = [[[VBRestKit sharedDataManager]objectManager] managedObjectStore];
NSManagedObjectContext *managedObjectContext  = managedObjectStore.persistentStoreManagedObjectContext;

NSFetchRequest *fetchRequest        = [[NSFetchRequest alloc] init];
NSEntityDescription *entity         = [NSEntityDescription
                                       entityForName:@"VBDeals" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortById = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortById]];
[fetchRequest setEntity:entity];

NSError *error;
NSArray *fetchedObjects             = [managedObjectContext executeFetchRequest:fetchRequest error:&error];


VBResponseWrapper *responseWrapper = [[VBResponseWrapper alloc]init];

responseWrapper.responseObject = [NSMutableArray arrayWithArray:fetchedObjects];

return responseWrapper;

您可以添加另一個排序描述符:

NSSortDescriptor *sortById = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortById, sortByName, nil]];

只要確保您更改密鑰(名稱)以匹配您的數據即可。 希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM