我得到一个包含以下字符串的字符串数组,这些字符串有一定的模式
str1 str51 str3 str4 str10 str39 str31 str191
每个字符串都以 'str' 开头,并在其末尾附加一个数字。
有没有一种巧妙的方法来对数组进行排序,以便按值顺序列出字符串
str1 str3 str4 str10 str31 str39 str51 str191
我可以通过编写一些递归函数来看到一种方法
NSString * firstString = [[strArray objectAtIndex:i].substringFromIndex:3];
NSString * secondString = [[strArray objectAtIndex:i+1].substringFromIndex:3];
if ([firstString intValue] < [secondString intValue])
{
//do nothing they order is correct
}
else
{
//swap the order of the 2 strings in the array
}
但那是非常基本的代码,Objective-C 中是否有一些机制或一个很好的代码技巧来更好地处理这种排序?
非常感谢,-代码