0

こんにちは、フェッチ結果のデッドエンド順序をソートする必要があります。これが私のコードです

NSManagedObjectContext *context = [appDelegate managedObjectContext]; 

NSError *error1;
NSEntityDescription *entityDesc;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
entityDesc=[NSEntityDescription entityForName:@"SubCategoryEntity" inManagedObjectContext:context];
[fetchRequest setEntity:entityDesc];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                            initWithKey:@"subCategoryId" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSArray *array = [context executeFetchRequest:fetchRequest error:&error1];

ここでは「サブカテゴリ」文字列タイプを使用しているため、「1 桁」では正しい順序で表示されますが、「2 桁」では機能しませんでした

ここでは、「11」「9」、「8」、「7」、「6」、「5」、「4」、「3」、「2」、「1」、「10」、 「0」

ここでは、「10」、「9」、「8」、「7」、「6」、「5」、「4」、「3」、「2」、「1」、「0」を表示する必要があります

なぜそれがハプニングなのかわからない 誰か助けてくれる?

よろしくお願いします。

4

1 に答える 1

1

これが文字列の並べ替えの仕組みであるため、この方法で順序を取得しています。のカスタムクラスNSSortDescriptorでカスタムcompareSubCategoryId:セレクターを使用してみることができます。SubCategorySubCategoryEntity

アップデート

次のようにソート記述子を初期化します。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"subCategoryId" 
                                                           ascending:NO
                                                            selector:@selector(compareSubCategoryId:)];

次に、カスタムNSManagedObjectサブクラスにメソッドを追加します。

- (NSComparisonResult)compareSubCategoryId:(id)otherObject {
  int ownSubCatId = [[self subCategoryId] intValue];
  int otherSubCatId = [[otherObject subCategoryId] intValue];

  if (ownSubCatId < otherSubCatId) return NSOrderedAscending;
  if (ownSubCatId > otherSubCatId) return NSOrderedDescending;
  return NSOrderedSame;
}
于 2012-06-05T09:23:00.443 に答える