2011-12-26 97 views
3

我有一個數組,其中包含像[email protected],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; 
} 

請建議如何爲數組做排序。

+0

有關字母數字排序的正確答案,請參閱本G回答這裏http://stackoverflow.com/questions/1351182/how-to-sort-a-nsarray-alphabetically – 2017-02-28 02:46:08

回答

8
NSArray *sry_img = [[NSArray alloc] initWithObjects:@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",nil]; 
    NSArray *sortedStrings = [sry_img sortedArrayUsingSelector:@selector(localizedStandardCompare:)]; 

NSLog(@"%@",sortedStrings); 

Enjy .......

但 localizedStandardCompare :,應該在文件名或其他字符串以類似Finder的列表和表格的形式出現時使用排序是適當的。此方法的確切行爲可能會在將來的版本中進行調整,並且在不同的本地化版本下會有所不同,因此客戶端不應該依賴字符串的確切排序順序。

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)首先,我寫了一個函數來獲取字符串中的@字符之前的數字,然後我實現了簡單的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方法中進行測試。

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"[email protected]",@"[email protected]", 
         @"[email protected]", @"[email protected]", 
         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); 

我希望它至少能讓你繼續。 :)