如何对NSArray键进行排序?

时间:2011-02-28 12:45:25

标签: ios objective-c cocoa-touch sorting nsarray

我有一个来自NSArray的{​​{1}}个密钥:

NSDictionary

为什么不对数组进行排序?

3 个答案:

答案 0 :(得分:3)

我认为它对你有用

NSDictionary *stateZip;
NSArray *states;
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];         
self.stateZip=dictionary;
[dictionary release];


NSArray *component = [self.stateZip allKeys];
NSArray *sorted =[component sortedArrayUsingSelector:@selector(compare:)];
self.states=sorted;

ragards, CNSivakUmar

答案 1 :(得分:0)

Heyy试试这个....

NSSortDescriptor *lastNameSorter = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];

所以我把它放在data.h / m文件中,因为NSMutablearray是用数据创建的,或者是在viewcontroller.h / m文件中,因为我需要在表中显示数据?

答案 2 :(得分:0)

这可能会有所帮助:

NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@”self” ascending:YES];
[array sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
[sortDesc release];

有关详细信息,请查看

  1. NSSortDescriptor
  2. 分拣-NSArrays