Created
January 2, 2013 20:03
-
-
Save codeswimmer/4437459 to your computer and use it in GitHub Desktop.
iOS: Array Sorting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Basic Array Sort In Ascending Order. | |
| [SomeArrayName sortUsingSelector:@selector(compare:)]; | |
| OR | |
| SomeArrayName = [SomeArrayName sortedArrayUsingSelector:@selector(compare:)];Sorting In Descending Order | |
| Sorting In Descending Order. | |
| NSSortDescriptor *sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO]; | |
| [SomeArrayName sortUsingDescriptors:[NSArray arrayWithObject: sortOrder]]; | |
| SomeArrayName = [SomeArrayName sortedArrayUsingDescriptors:[NSArray arrayWithObject: sortOrder]]; | |
| Sorting Array Of Dictionary By Key Value. | |
| This function return a sorted array with dictionary by key value. | |
| NSArray* sortArray(NSMutableArray *array, BOOL isAscendingYesNo, NSString* key){ | |
| NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:key ascending:isAscendingYesNo selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; | |
| NSArray *descriptorsArray = [NSArray arrayWithObjects:sortDescriptor, nil]; | |
| NSArray *sortedArray = [array sortedArrayUsingDescriptors:descriptorsArray]; | |
| return sortedArray; | |
| } | |
| Usage: | |
| SomeArrayName = sortArray(SomeArrayName, YES, @"name"); | |
| Above example display an array of dictionary sorted by "name " key. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment