对 NSDictionary 对象的 NSArray 进行排序的最佳方法? [英] Best way to sort an NSArray of NSDictionary objects?

查看:27
本文介绍了对 NSDictionary 对象的 NSArray 进行排序的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力尝试对字典数组进行排序.

I'm struggling with trying to sort an array of dictionaries.

我的字典有几个值,如兴趣、价格、受欢迎程度等.

My dictionaries have a couple of values of interest, price, popularity etc.

有什么建议吗?

推荐答案

像这样使用 NSSortDescriptor ..

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

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

stories is the array you want to sort. recent is another mutable array which has sorted dictionary values. Change the @"interest" with the key value on which you have to sort.

一切顺利

这篇关于对 NSDictionary 对象的 NSArray 进行排序的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆