[英]Best way to sort an NSArray of custom objects by an alphanumerical property
As I say in the title, I have an NSArray
of objects which in turn have an NSString
property with this format (I provide a kind of regex to explain): [1-9]+[AZ]* 就像我在标题中所说的,我有一个对象的
NSArray
,这些对象又具有这种格式的NSString
属性(我提供一种正则表达式来解释):[1-9] + [AZ] *
I'd like to sort such array firstly by the first numerical characters, and then by the last alphabetical characters, for example: if I have 6 objects with the strings [@"1", @"1A", @"3", @"2D", @"10B", @"1B"], I'd want to get the sorted array [@"1", @"1A", @"1B", @"2D", @"3", @"10B"]. 我想首先按第一个数字字符然后按最后一个字母字符对这种数组进行排序,例如:如果我有6个对象,它们的字符串为[@“ 1”,@“ 1A”,@“ 3”, @“ 2D”,@“ 10B”,@“ 1B”],我想获取排序后的数组[@“ 1”,@“ 1A”,@“ 1B”,@“ 2D”,@“ 3” ,@“ 10B”]。
Since I want to sort by a property that is an NSString
, how could I achieve that? 由于我想按
NSString
属性进行排序,我该如何实现?
Thanks in advance 提前致谢
There is a very easy approach available to do the same stuff: 有一种非常简单的方法可以用来做同样的事情:
NSArray *unsortedArray = @[@"1", @"1A", @"3", @"2D", @"10B", @"1B"];
NSArray *sortedArray = [unsortedArray sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
NSLog(@"%@",sortedArray);
Enjoy ur coding....:) 享受我们的编码。...:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.