使用获取的请求sorting描述符

我有我的sorting描述符的问题。 我正在获取一个足球俱乐部的排名。 这是我的web服务如何给数据返回。

"ranking": [ { "type": "competitie", "position": "1", "name": "Club Brugge", "gamesPlayed": "10", "gamesWon": "6", "gamesTied": "4", "gamesLost": "0", "goalsPos": "25", "goalsNeg": "12", "goalsDiff": 13, "points": "22" } 

这是我的提取请求。

 - (void)getKlassement // attaches an NSFetchRequest to this UITableViewController { NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Klassement"]; request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES]]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.genkDatabase.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; } 

现在排名如下。 1,11,12,13,14,2,3,4,5 …我认为问题是我的web服务中的位置是一个string。 而我的sorting描述符正在把它看作一个string。 但实际上它应该把它看作一个整数。 有谁知道我怎么能做到这一点?

提前致谢。

即使“位置”是作为一个string从您的Web服务提供,最好的解决scheme是将其存储为核心数据模型中的数字(“整数32”或类似的)。 该对象的相应属性将是NSNumber ,它将被正确sorting。

如果你真的不能这样做,你可以作为一个解决方法尝试使用localizedStandardCompare作为比较函数。 我认为这种string包含数字根据他们的数字值。

 [NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES selector:@selector(localizedStandardCompare:)]