2012-06-05 52 views
0

您好,我需要排序结果取这里deasending顺序是我的代码如何排序核心数据读取结果

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”

我不是为什么它的攻丝可以帮助我的任何人

感谢您的提前。

回答

1

您以这种方式获得订单,因为这是字符串排序的工作方式。您可以在SubCategoryEntity的定制SubCategory类中尝试使用NSSortDescriptor与自定义compareSubCategoryId:选择器。

更新

初始化你的某种描述是这样的:

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; 
} 
+0

感谢响应可以请你跟我说清楚请 ? –