版权声明:本文为GodBlessMyParents原创文章,未经博主允许不得转载。
// Copyright (c) 2015年 JWC. All rights reserved.
返回:@"Laabbcccdeeeeeeeefggiiiiiiikkklllmmnnnoooopprrssttuuuvyyy"
*/
-(NSString *)sortString:(NSString *)str{
NSMutableString *muString = [NSMutableString stringWithString:str];
//冒泡排序
for (int i = 0; i < muString.length - 1; i++) {
for (int j = 0; j < muString.length - i - 1; j++) {
if ([muString characterAtIndex:j] > [muString characterAtIndex:j+1]) {
//把第j+1的值取出来赋值给p
unichar p = [muString characterAtIndex: j+1];
//用第j为替换第j+1位
[muString replaceCharactersInRange:NSMakeRange(j+1, 1) withString:[NSString stringWithFormat:@"%c",[muString characterAtIndex:j]] ];
//用第j+1位的值替换给第j位,完成交换
[muString replaceCharactersInRange:NSMakeRange(j, 1) withString:[NSString stringWithFormat:@"%c",p]];
}
}
}
return muString;
}