<分区>
Possible Duplicate:
What is the most efficient way to sort an NSSet?
我目前有一个 NSMutableSet,其中包含以下字符串列表。例如。: {@"c",@"d",@"e",@"f",@"g",@"a"}
有人可以告诉我如何按字母顺序对这些值进行排序吗?有没有办法使用 sortedArrayUsingDescriptors:
来做到这一点?
<分区>
Possible Duplicate:
What is the most efficient way to sort an NSSet?
我目前有一个 NSMutableSet,其中包含以下字符串列表。例如。: {@"c",@"d",@"e",@"f",@"g",@"a"}
有人可以告诉我如何按字母顺序对这些值进行排序吗?有没有办法使用 sortedArrayUsingDescriptors:
来做到这一点?
最佳答案
当然,你可以使用那个方法:
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES];
NSArray *sortedArray = [mySet sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
这只是创建了一个新的排序描述符,它指定了键“description”(NSString 提供了一个返回字符串的-description
方法)。然后它将包含该排序描述符的数组作为参数传递给 -sortedArrayUsingDescriptors:
,您可以将其发送到任何 NSSet。
关于iphone - objective-c 对 NSStrings 的 NSMutableSet 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8608862/