【问题标题】:How can I sort an NSMutableArray alphabetically?如何按字母顺序对 NSMutableArray 进行排序?
【发布时间】:2011-01-18 11:49:58
【问题描述】:

我想按字母顺序对 NSMutableArray 进行排序。

【问题讨论】:

  • 数组中对象的类型是什么?
  • 我将一个对象存储在数组中,我想根据 object.name 字段对数组进行排序

标签: iphone objective-c nsmutablearray


【解决方案1】:

您可以这样做来对 NSMutableArray 进行排序:

[yourArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

【讨论】:

  • [tempPeoples sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];我使用了这段代码,它给出了异常
  • 能否请您发布异常声明。
  • NAddressBook[5197:207] *** -[AddressDatalocalizedCaseInsensitiveCompare:]:无法识别的选择器发送到实例 0x149160 2011-01-18 17:01:14.470 NAddressBook[5197:207] *** 终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:“***-[AddressDatalocalizedCaseInsensitiveCompare:]:无法识别的选择器发送到实例 0x149160”2011-01-18 17:01:14.484 NAddressBook[5197:207] 堆栈:(跨度>
  • 也许对象不是 NSString 类型...编辑:是 :-)
  • 好的,它不是字符串,问题是我将对象存储在数组中,该对象包含一个字段作为名称,我想对数组进行排序
【解决方案2】:

此处提供的其他答案提到使用 @selector(localizedCaseInsensitiveCompare:)
这对于 NSString 数组非常有用,但是 OP 评论说该数组包含对象并且应该根据 object.name 属性进行排序。
在这种情况下,您应该这样做:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];

您的对象将根据这些对象的名称属性进行排序。

【讨论】:

  • 正是我需要的!谢谢!
  • 这是这个特定问题的更好答案。效果很好!
【解决方案3】:
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; // Describe the Key value using which you want to sort. 
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor]; // Add the value of the descriptor to array.
sortedArrayWithName = [yourDataArray sortedArrayUsingDescriptors:descriptors]; // Now Sort the Array using descriptor.

在这里你会得到排序后的数组列表。

【讨论】:

    【解决方案4】:

    在最简单的场景中,如果你有一个字符串数组:

    NSArray* data = @[@"Grapes", @"Apples", @"Oranges"];
    

    如果你想对它进行排序,你只需传入 nil 作为描述符的键,然后调用我上面提到的方法:

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
    data = [data sortedArrayUsingDescriptors:@[descriptor]];
    

    输出如下所示:

    Apples, Grapes, Oranges
    

    更多详情请查看this

    【讨论】:

      【解决方案5】:

      使用NSSortDescriptor 类并休息,你会得到所有东西here

      【讨论】:

        【解决方案6】:
        NSSortDescriptor * sortDescriptor;
        sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name_your_key_value" ascending:YES];
        NSArray * sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
        NSArray * sortedArray;
        sortedArray = [Your_array sortedArrayUsingDescriptors:sortDescriptors];
        

        【讨论】:

          【解决方案7】:

          也许这可以帮助你:

          [myNSMutableArray sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES],[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES]]];
          

          所有都是根据 NSSortDescriptor...

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-02-12
            • 2011-08-29
            • 2010-11-23
            • 1970-01-01
            • 1970-01-01
            • 2017-05-22
            • 1970-01-01
            相关资源
            最近更新 更多