Skip to content

Instantly share code, notes, and snippets.

@acidsound
Created March 2, 2013 17:57
Show Gist options
  • Save acidsound/5072229 to your computer and use it in GitHub Desktop.
Save acidsound/5072229 to your computer and use it in GitHub Desktop.
objective-c에서 한글>영문>숫자 순으로 정렬.
@implementation NSString (Support)
- (NSComparisonResult) sortForIndex:(NSString*)comp {
// 기본 localizedCaseInsensitiveCompare는 숫자, 영문(대소무시), 한글 순 정렬
// 한글 > 영문(대소구분 없음) > 숫자 > $
// 그외 특수문자는 전부 무시한채 인덱싱
// $는 예외
// self 가 @"ㄱ" 보다 작고 (한글이 아니고) , comp 가 @"ㄱ"보다 같거나 클때 - 무조건 크다
// 비교하면 -1 0 1 이 작다, 같다, 크다 순이므로 +1 을 하면 한글일때 YES 아니면 NO 가 된다.
// self 가 한글이고 comp 가 한글이 아닐때 무조건 작다인 조건과
// self 가 글자(한/영)이 아니고 comp가 글자(한/영)일떄 무조건 크다인 조건을 반영한다.
NSString* left = [NSString stringWithFormat:@"%@%@",
[self localizedCaseInsensitiveCompare:@"ㄱ"]+1 ? @"0" :
!([self localizedCaseInsensitiveCompare:@"a"]+1) ? @"2" :
@"1", self];
NSString* right = [NSString stringWithFormat:@"%@%@",
[comp localizedCaseInsensitiveCompare:@"ㄱ"]+1 ? @"0" :
!([comp localizedCaseInsensitiveCompare:@"a"]+1) ? @"2" :
@"1", comp];
return [left localizedCaseInsensitiveCompare:right];
}
@end
@implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
phoneBook = [[NSArray arrayWithObjects:
@"ㄱ",
@"힣",
@"가나",@"가봉", @"Korea",
@"나이지리아", @"뭄바이",
@"히말라야", @"a", @"A", @"Africa",@"100 rogues", @"0", @"1",
@"37 signals",
nil] sortedArrayUsingSelector:@selector(sortForIndex:)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment