티스토리 뷰
리스트 같은것을 표현을 할 경우에, 정렬을 하여야 한다.
정렬 방법에 대해서 이야기 하도록 한다.
1. NSSortDescriptor 이용
NSString *LAST = @"lastName";
NSString *FIRST = @"firstName";
NSMutableArray *array = [NSMutableArray array];
NSArray *sortedArray;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"A",FIRST,@"1",LAST,nil];
[array addObject:dict];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"B",FIRST,@"2",LAST,nil];
[array addObject:dict];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"C",FIRST,@"3",LAST,nil];
[array addObject:dict];
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc]
initWithKey:LAST
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *firstNameDescriptor = [[NSSortDescriptor alloc]
initWithKey:FIRST
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObjects:lastNameDescriptor, firstNameDescriptor, nil];
sortedArray = [array sortedArrayUsingDescriptors:descriptors];
* NSSortDescriptor의 첫번째를 비교한후에, 같은 값일 경우에는 다음 조건을 비교한다.
2. Block 정렬
NSArray *sortedArray = [array sortedArrayUsingComparator:^(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue])
return (NSComparisonResult)NSOrderDescending;
else if ([obj1 integerValue] < [obj2 integerValue] )
return (NSComparisonResult)NSOrderAscending;
else
return (NSComparisonResult)NSOrderSame;
}];
* 오름차순을 기준으로 하였음.
'Mobile > iOS' 카테고리의 다른 글
[iOS] Image Resize (0) | 2016.08.01 |
---|---|
[iOS] 일정시간 이후에 Method 호출하기 (0) | 2016.08.01 |
[iOS] 해상도 관련. (0) | 2016.07.28 |
[iOS]ARC 설정 (0) | 2016.05.14 |
[iOS]ScrollView Scroll Delegate 순서 (0) | 2016.04.26 |
- Total
- Today
- Yesterday
- CentOS
- Gradle
- Python
- tomcat
- git
- rxswift
- Linux
- golang
- enum
- Codable
- MySQL
- intellij
- SWIFT
- docker
- ubuntu
- centos8
- cocoapods
- github
- nodejs
- Kotlin
- Spring
- android
- Java
- windows10
- php
- war
- ios
- go
- Xcode
- Windows
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |