嗨我需要排序获取结果deasending order这里是我的代码
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];
这里我使用“子类别”字符串类型,因此它以“单个数字”显示正确的顺序,但它无法在“双数字”中工作
这是我在计算“11”后得到的订单 “9”, “8”, “7”, “6”, “5”, “4”, “3”, “2”, “1”, “10”, “0”
这里我需要显示“10”,“9”,“8”,“7”,“6”,“5”,“4”,“3”,“2”,“1”,“0 “
我不知道为什么它可以帮助我
感谢您提前。
答案 0 :(得分:1)
您以这种方式获得订单,因为这是字符串排序的工作方式。您可以尝试在NSSortDescriptor
的自定义compareSubCategoryId:
课程中使用SubCategory
自定义SubCategoryEntity
选择器。
<强>更新强>
按如下方式初始化排序描述符:
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;
}