如何使用字母数字值对数组进行排序?

时间:2011-12-26 07:52:53

标签: iphone objective-c sorting

我有一个包含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;
}

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

4 个答案:

答案 0 :(得分: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 :.此方法的确切行为可能会在将来的版本中进行调整,并且在不同的本地化版本下会有所不同,因此客户端不应该依赖于字符串的确切排序顺序。

答案 1 :(得分: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];

答案 2 :(得分:0)

你可以试试这个 -

NSString *str1 = [[[[str1 componentsSeparatedByString:@"frame_"] objectAtIndex:1]       componentsSeparatedByString:@"@3x.png"] objectAtIndex:0];
int num1 = [str1 integerValue];

答案 3 :(得分:0)

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

1)首先我编写了一个函数来获取字符串中@字符前面的数字然后我实现了简单的SELECTION SORT算法来使用这个函数对数组进行排序。

- (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方法中的代码复制到test。

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);

我希望它至少可以让你继续前进。 :)