如何按字母顺序对NSArray进行排序?

Dot*_*ash 374 sorting objective-c

如何[UIFont familyNames]按字母顺序对填充的数组进行排序?

Tho*_*ing 721

最简单的方法是提供排序选择器(Apple的文档以获取详细信息)

Objective-C的

sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Run Code Online (Sandbox Code Playgroud)

迅速

let descriptor: NSSortDescriptor = NSSortDescriptor(key: "YourKey", ascending: true, selector: "localizedCaseInsensitiveCompare:")
let sortedResults: NSArray = temparray.sortedArrayUsingDescriptors([descriptor])
Run Code Online (Sandbox Code Playgroud)

Apple提供了几种字母排序选择器:

  • compare:
  • caseInsensitiveCompare:
  • localizedCompare:
  • localizedCaseInsensitiveCompare:
  • localizedStandardCompare:

迅速

var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
students.sort()
print(students)
// Prints "["Abena", "Akosua", "Kofi", "Kweku", "Peter"]"
Run Code Online (Sandbox Code Playgroud)

参考

  • 我更新了链接.感谢您指出了这一点.localizedCaseInsensitiveCompare:是一个NSString的方法,应该足以对字符串数组进行排序. (34认同)
  • 文档链接已过时,代码示例实际上不足以对数组进行实际排序.localizedCaseInsensitiveCompare:需要以某种方式定义. (5认同)

JP *_*sek 275

这里提供的其他答案提到使用@selector(localizedCaseInsensitiveCompare:) 这适用于NSString数组,但是如果你想将它扩展到另一种类型的对象,并根据'name'属性对这些对象进行排序,你应该这样做:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
sortedArray=[anArray sortedArrayUsingDescriptors:@[sort]];
Run Code Online (Sandbox Code Playgroud)

您的对象将根据这些对象的name属性进行排序.

如果您希望排序不区分大小写,则需要像这样设置描述符

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
Run Code Online (Sandbox Code Playgroud)

  • +1,谢谢.有趣的是,这是比通常的答案更常见的用例. (14认同)
  • 这首先是大写字母,然后是小写字母 (4认同)

Ben*_*n G 27

一种更强大的方法来排序NSString列表以使用NSNumericSearch之类的东西:

NSArray *sortedArrayOfString = [arrayOfString sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
        }];
Run Code Online (Sandbox Code Playgroud)

结合SortDescriptor,可以得到如下结果:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
    }];
NSArray *sortedArray = [anArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
Run Code Online (Sandbox Code Playgroud)


Jay*_*bey 10

使用下面的代码按字母顺序排序:

    NSArray *unsortedStrings = @[@"Verdana", @"MS San Serif", @"Times New Roman",@"Chalkduster",@"Impact"];

    NSArray *sortedStrings =
    [unsortedStrings sortedArrayUsingSelector:@selector(compare:)];

    NSLog(@"Unsorted Array : %@",unsortedStrings);        
    NSLog(@"Sorted Array : %@",sortedStrings);
Run Code Online (Sandbox Code Playgroud)

以下是控制台日志:

2015-04-02 16:17:50.614 ToDoList[2133:100512] Unsorted Array : (
    Verdana,
    "MS San Serif",
    "Times New Roman",
    Chalkduster,
    Impact
)

2015-04-02 16:17:50.615 ToDoList[2133:100512] Sorted Array : (
    Chalkduster,
    Impact,
    "MS San Serif",
    "Times New Roman",
    Verdana
)
Run Code Online (Sandbox Code Playgroud)


Lis*_*ien 10

另一种对字符串数组进行排序的简单方法是使用NSString description属性:

NSSortDescriptor *valueDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES];
arrayOfSortedStrings = [arrayOfNotSortedStrings sortedArrayUsingDescriptors:@[valueDescriptor]];
Run Code Online (Sandbox Code Playgroud)