【问题标题】:How to sort an NSArray of keys?如何对键的 NSArray 进行排序?
【发布时间】:2011-02-28 12:45:25
【问题描述】:

我有一个来自NSDictionary 的密钥NSArray,如下所示:

NSArray *array = [[NSArray alloc] initWithArray:[myDict allKeys]];
[array sortedArrayUsingSelector:@selector(compare:)];

为什么数组没有排序?

【问题讨论】:

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


    【解决方案1】:

    我认为它对你有用

    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;
    

    告白, CNSivakUmar

    【讨论】:

      【解决方案2】:

      嘿,试试这个....

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

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

      【讨论】:

        【解决方案3】:

        这可能会有所帮助:

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

        欲了解更多信息,请查看

        1. NSSortDescriptor
        2. 排序-NSArrays

        【讨论】:

        • 不起作用... [__NSArrayI sortUsingDescriptors:]:无法识别的选择器发送到实例 0x6a58bc0
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        • 2011-02-20
        相关资源
        最近更新 更多