如何排序 NSArray 字母顺序?

How to sort NSArray alphabetical order?

我有一个数组,我在其中列出了数据,如下所示。

[directoryContent sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]

但输出是,

<__NSArrayI 0x16739870>(
 bedsheet0.jpg,
 bedsheet1.jpg,
 bedsheet10.jpg,
 bedsheet11.jpg,
 bedsheet2.jpg,
 bedsheet3.jpg,
 bedsheet4.jpg,
 bedsheet5.jpg,
 bedsheet6.jpg,
 bedsheet7.jpg,
 bedsheet8.jpg,
 bedsheet9.jpg

)

但是我需要像下面这样对这个列表进行排序,

<__NSArrayI 0x16739870>(
 bedsheet0.jpg,
 bedsheet1.jpg,
 bedsheet2.jpg,
 bedsheet3.jpg,
 bedsheet4.jpg,
 bedsheet5.jpg,
 bedsheet6.jpg,
 bedsheet7.jpg,
 bedsheet8.jpg,
 bedsheet9.jpg
 bedsheet10.jpg,
 bedsheet11.jpg,

)

我该怎么做?

提前致谢!

使用NSNumericSearch搜索字符串中的数值,如下所示:-

NSArray * products = [[NSArray alloc] initWithObjects:@"Product1",@"Product10",@"Product2",@"Product3",@"Product4",@"Product5",@"Product6",@"Product7",@"Product8",@"Product9", nil];

products = [products sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];

NSLog(@"products : %@", products);

您可以使用排序描述符来完成。

NSArray *unsortedArray = [NSArray arrayWithObjects:@"12", @"4", @"89", @"5", @"2", @"10", @"1", nil]; NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(NSString *str1, NSString *str2) { return [str1 compare:str2 options:NSNumericSearch]; }];