我实际上能够找出解决方案。我NSComparisonResult
使用自定义逻辑创建了一个块来读取每个字符串前面的数字部分,然后对它们进行数字比较:
NSComparisonResult (^sortByNumber)(id, id) = ^(id obj1, id obj2)
{
//Convert items to strings
NSString *s1 = (NSString *)obj1;
NSString *s2 = (NSString *)obj2;
//Find the period and grab the number
NSUInteger periodLoc1 = [s1 rangeOfString:@"."].location;
NSString *number1 = [s1 substringWithRange:NSMakeRange(0, periodLoc1)];
NSUInteger periodLoc2 = [s2 rangeOfString:@"."].location;
NSString *number2 = [s2 substringWithRange:NSMakeRange(0, periodLoc2)];
//Compare the numeric values of the numbers
return [number1 compare:number2 options:NSNumericSearch];
};
然后我通过调用对我的数组进行排序:
NSArray *array = [[[self myDictionary] allValues] sortedArrayUsingComparator:sortByNumber];