在数组排序字典? [英] Sort Dictionaries in Array?

查看:85
本文介绍了在数组排序字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要排序nsdictionaries的一个NSMutableArray,使用密钥的对象从字典。对象是一个NSNumber。如何将我的第一个对象是最高的NSNumber在NSMutableArray里的字典排序?

I need to sort an nsmutablearray of nsdictionaries, using a keyed object from the dictionary. The object is an NSNumber. How would I sort the dictionaries in the nsmutablearray with the first object being the highest nsnumber?

谢谢!

推荐答案

通过使用<定义一个定制的比较href=\"http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/_index.html\">block (你可以去其他的方式,但这是相当紧凑):

With a custom comparator defined with a block (you can go other ways but this is quite compact):

NSComparator mysort = ^(id dict1, id dict2) {
    NSNumber n1 = [dict1 objectForKey:@"key"];
    NSNumber n2 = [dict2 objectForKey:@"key"];
    return (NSComparisonResult)[n1 compare:n2];
};

[yourArray sortUsingComparator:mysort];

这篇关于在数组排序字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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