iOS:按字母顺序重新排序NSString字符

时间:2016-04-17 06:30:47

标签: ios objective-c

Objective-C中是否有一种方法可以按字母顺序重新排序字符串的字符?

例如:

  

输入:" fhwoijc"

     

输出:" cfhijow"

这就是我想出来的 - 虽然它已经做了很多工作

// Convert NSString to NSArray
NSMutableArray *letterArray = [NSMutableArray array];
NSString *letters = @"fhwoijc";
[letters enumerateSubstringsInRange:NSMakeRange(0, [letters length]) 
                            options:(NSStringEnumerationByComposedCharacterSequences) 
                         usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    [letterArray addObject:substring];
}];

// Sort Array of strings alphabetically
[letterArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

// Convert Array back to NSString
NSString * result = [letterArray componentsJoinedByString:@""];

0 个答案:

没有答案