我有一个包含10个项目的数组,并希望根据NSNumber
值进行排序。此数组中的对象是Core Data对象。下面是我为获取排序数组而编写的代码。
问题是排序仅适用于两个对象,而不是全部。我做错了什么?
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"OrderList" inManagedObjectContext:context]];
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"orderNumber" ascending:YES]; //selector:@selector(localizedStandardCompare:)];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
orderNumArray = [context executeFetchRequest:request error:nil];
orderList = [orderNumArray copy];
[self.orderTable reloadData];
//排序前
1005 1004 1002 1001 1003
//排序后
1001 1002 1003 1004 1005
//表加载时
1004 1005 1002 1001 1003
//加载表
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *ordersCell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (ordersCell==nil) {
ordersCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
ordersCell.backgroundColor = [UIColor clearColor];
OrderList *orderListNew1 = [orderList objectAtIndex:indexPath.row];
// orderListNew1.orderNumber is used in the UILabel.
return ordersCell;
}