我有一系列代表以下文件名的字符串:hello1(eng).txt
, hello1(por).txt
, hello1.txt
。当我使用
NSArray *array = [filePaths sortedArrayUsingSelector: @selector(compare:)];
返回的顺序是hello1(eng).txt, hello1(por).txt, hello1.txt
。相反,我想要这样的顺序,其中括号中的项目最后返回:hello1.txt、hello1(eng).txt、hello1(por).txt
。
如何自定义排序行为以提供此排序?
最佳答案
sortedArrayUsingSelector:
告诉数组询问它尝试排序的各个对象的适当排序顺序。要提供您自己的,请使用其他 sortedArrayUsing...
函数之一。最简单的任务可能是 sortedArrayUsingFunction:context:它允许您指定一个普通的旧 C 函数来进行比较。
另一种方法是使用类别向 NSString 类添加排序功能,类似于 compareInMyFavor:
并将其作为选择器传递给 sortedArrayUsingSelector:
.
现在我想到了,后者可能是更清洁的解决方案。你来电。
关于iphone - 如何自定义 NSArray 字符串的排序顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6817901/