sorting的NSArray的NSDictionary对象的最佳方法?

我努力尝试sorting字典数组。

我的字典有一些价值的兴趣,价格,知名度等

有什么build议么?

像这样使用NSSortDescriptor

 NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest" ascending:YES]; stories = [stories sortedArrayUsingDescriptors:@[descriptor]]; recent = [stories copy]; 

stories是你想sorting的数组。 recent是另一个可变数组,它对字典值进行了sorting。 用您必须对其进行sorting的关键值更改@"interest"

祝一切顺利

 [array sortUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"YOUR-KEY" ascending:YES], nil]]; 

我真的不想把它分成多行。 这对我来说已经够简单了。

您可以从父项遍历到所需的子项目。 例如

 NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"parent.child.child" ascending:YES]; 

更新,sortUsingDescriptors现在是sortedArrayUsingDescriptors

所以,就像:

 items = [items sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]]; 

编写一个sorting函数,比较两个字典中的相关字段。 当你有这个版本,你可以例如使用NSArray#sortedArrayUsingFunction:context:来sorting你的数组。

请参阅NSArray类参考

 array = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"YOUR-KEY" ascending:YES], nil]]; 

记住分配给数组