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

时间:2022-01-20 10:04:45

I have an array which contains strings like frame_10@3x.png , frame_5@3x.png,frame_19@3x.png etc. So I want to sort this array according to the number after the underscore i.e. the correct sequence will be frame_5@3x.png,frame_10@3x.png,frame_19@3x.png.

我有一个包含像frame_10@3x这样的字符串的数组。png,frame_5@3x.png,frame_19@3x。所以我想根据下划线后的数字对这个数组进行排序,即正确的序列是frame_5@3x.png,frame_10@3x.png,frame_19@3x.png。

I tried to use the following method but no result:

我尝试使用以下方法,但没有结果:

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

Please suggest how to do this sorting for array.

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

4 个解决方案

#1


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 .......

Enjy .......

But localizedStandardCompare:, added in 10.6, should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact behavior of this method may be tweaked in future releases, and will be different under different localizations, so clients should not depend on the exact sorting order of the strings.

但是本地化的standardcompare:,在10.6中添加,当在列表和表中显示文件名或其他字符串时,应该使用它,因为类似查找器的排序是合适的。这个方法的确切行为可以在以后的版本中进行调整,并且在不同的本地化下会有所不同,因此客户机不应该依赖于字符串的精确排序顺序。

#2


0  

you want to do something like:

你想做的是:

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

#3


0  

I am not sure if my solution is the best possible approach but it can solve your problem for the time being :) .

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

1) First I have written a function to get the numbers before @ character in your string and then I implemented simple SELECTION SORT algo to sort the array using this functions.

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

- (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;
}

This is the selection Implementation of the algo for sorting. The main logic is in the for loop. You can copy the code in viewDidLoad method to test.

这是用于排序的algo的选择实现。主要逻辑在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);

I hope that it can atleast keep you going. :)

我希望它至少能让你坚持下去。:)

#4


0  

You can try this-

你可以试试这个

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

#1


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 .......

Enjy .......

But localizedStandardCompare:, added in 10.6, should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact behavior of this method may be tweaked in future releases, and will be different under different localizations, so clients should not depend on the exact sorting order of the strings.

但是本地化的standardcompare:,在10.6中添加,当在列表和表中显示文件名或其他字符串时,应该使用它,因为类似查找器的排序是合适的。这个方法的确切行为可以在以后的版本中进行调整,并且在不同的本地化下会有所不同,因此客户机不应该依赖于字符串的精确排序顺序。

#2


0  

you want to do something like:

你想做的是:

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

#3


0  

I am not sure if my solution is the best possible approach but it can solve your problem for the time being :) .

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

1) First I have written a function to get the numbers before @ character in your string and then I implemented simple SELECTION SORT algo to sort the array using this functions.

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

- (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;
}

This is the selection Implementation of the algo for sorting. The main logic is in the for loop. You can copy the code in viewDidLoad method to test.

这是用于排序的algo的选择实现。主要逻辑在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);

I hope that it can atleast keep you going. :)

我希望它至少能让你坚持下去。:)

#4


0  

You can try this-

你可以试试这个

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