排序NSD阵列NSDrray对象的最佳方法?

iOS*_*vil 68 sorting objective-c nsdictionary nsarray ios

我正在努力尝试排序一系列字典.

我的词典有一些兴趣,价格,受欢迎程度等值.

有什么建议?

War*_*ior 200

NSSortDescriptor像这样使用..

NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest" ascending:YES];
stories = [stories sortedArrayUsingDescriptors:@[descriptor]];
recent = [stories copy];
Run Code Online (Sandbox Code Playgroud)

stories是要排序的数组.recent是另一个可变数组,它已经对字典值进行了排序.@"interest"使用您必须排序的键值更改.

祝一切顺利

  • 这很好,但它确实泄漏了.1)应该释放或自动释放排序描述符,以及2)不需要复制调用就可以删除该行. (12认同)
  • NSSortDescriptor*descriptor = [[[NSSortDescriptor alloc] initWithKey:@"interest"ascending:YES] autorelease]; [故事sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]]; (5认同)
  • 您可以使用NSSortDescriptor*descriptor = [NSSortDescriptor sortDescriptorWithKey:@"interest"升序:YES]; (2认同)

sup*_*org 25

[array sortUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"YOUR-KEY" ascending:YES], nil]];
Run Code Online (Sandbox Code Playgroud)

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


Ans*_*ais 11

您只需从父级遍历到所需的子属性即可.例如

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"parent.child.child"  ascending:YES];
Run Code Online (Sandbox Code Playgroud)