如何对包含字母数字值的数组进行排序?

3
我有一个包含字符串的数组,例如 frame_10@3x.png、frame_5@3x.png、frame_19@3x.png 等等。 我想要按照下划线后面的数字对这个数组进行排序,也就是正确的顺序应该是 frame_5@3x.png、frame_10@3x.png、frame_19@3x.png。
我尝试使用以下方法但没有结果:
NSInteger firstNumSort(id str1, id str2, void *context) {
    int num1 = [str1 integerValue];
    int num2 = [str2 integerValue];

    if (num1 < num2)
        return NSOrderedAscending;
    else if (num1 > num2)
        return NSOrderedDescending;

    return NSOrderedSame;
}

请建议如何对数组进行排序。

有关字母数字数组排序的正确答案,请参见Ben G在此处的回答https://dev59.com/tnM_5IYBdhLWcg3wgjW2 - Sam B
4个回答

8
NSArray *sry_img = [[NSArray alloc]    initWithObjects:@"frame_18@3x.png",@"frame_17@3x.png",@"frame_1222@3x.png",@"frame_10@3x.png",@"frame_3@3x.png",@"frame_4@3x.png",@"frame_4@3x.png",@"frame_1@3x.png",@"frame_4@3x.png",@"frame_4@3x.png",nil];
  NSArray *sortedStrings  = [sry_img sortedArrayUsingSelector:@selector(localizedStandardCompare:)];

NSLog(@"%@",sortedStrings);

Enjy .......

但是,当文件名或其他字符串在需要类似Finder排序的列表和表格中呈现时,应该使用在10.6中添加的localizedStandardCompare:。该方法的确切行为可能会在将来的发布中进行调整,并且在不同的本地化下会有所不同,因此客户端不应依赖于字符串的精确排序顺序。


0

你想做类似这样的事情:

NSArray *components1 = [str1 componentsSeparatedByString:@"_"];
NSArray *components2 = [str2 componentsSeparatedByString:@"_"];

NSString *number1String = [components1 objectAtIndex:([components1 count] - 1])];
NSString *number2String = [components2 objectAtIndex:([components2 count] - 1])];

return [number1String compare:number2String];

0
你可以尝试这个 -
NSString *str1 = [[[[str1 componentsSeparatedByString:@"frame_"] objectAtIndex:1]       componentsSeparatedByString:@"@3x.png"] objectAtIndex:0];
int num1 = [str1 integerValue];

0

我不确定我的解决方案是否是最佳方法,但它可以暂时解决您的问题 :)

1) 首先,我编写了一个函数来获取字符串中@字符之前的数字,然后我使用简单的选择排序算法来使用此函数对数组进行排序。

- (NSString*)getSubStringForString:(NSString*)value {
// First we will cut the frame_ string
NSMutableString *trimmedString = [NSMutableString stringWithString:[value substringWithRange:NSMakeRange(6, [value length]-6)]];

// New String to contain the numbers
NSMutableString *newString = [[NSMutableString alloc] init];

for (int i = 0; i < [trimmedString length] ; i++) {        
    NSString *singleChar = [trimmedString substringWithRange:NSMakeRange(i, 1)];
    if (![singleChar isEqualToString:@"@"]) {
        [newString appendString:singleChar];
    } else {
        break;
    }
}    
return newString;
}

这是排序算法的选择实现。主要逻辑在for循环中。您可以复制代码到viewDidLoad方法中进行测试。

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"frame_10@3x.png",@"frame_5@3x.png",
                         @"frame_3@3x.png", @"frame_19@3x.png",
                         nil];    
NSLog(@"Values before Sort: %@", array);

int iPos;
int iMin;

for (iPos = 0; iPos < [array count]; iPos++)
{

    iMin = iPos;

    for (int i = iPos+1; i < [array count]; i++)
    {            

        if ([[self getSubStringForString:[array objectAtIndex:i]] intValue] > 
            [[self getSubStringForString:[array objectAtIndex:iMin]] intValue]) {
            iMin = i;
        }

    }

    if ( iMin != iPos )
    {
        NSString *tempValue = [array objectAtIndex:iPos];
        [array replaceObjectAtIndex:iPos withObject:[array objectAtIndex:iMin]];
        [array replaceObjectAtIndex:iMin withObject:tempValue];

    }
}

NSLog(@"Sorted Values: %@", array);

希望它至少能让你继续前进。:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接