Created
May 23, 2012 04:34
-
-
Save hanksudo/2773298 to your computer and use it in GitHub Desktop.
Sort NSArray using sortedArrayUsingComparator, number last.
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
- (NSArray *)sortArrayNumberLast:(NSArray *)arr { | |
return [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { | |
NSString *s1 = [obj1 substringToIndex:1]; | |
NSString *s2 = [obj2 substringToIndex:1]; | |
if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == [s2 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location) { | |
return [obj1 compare:obj2]; | |
} else { | |
if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == NSNotFound) { | |
return NSOrderedAscending; | |
} | |
} | |
return NSOrderedDescending; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment