Created
November 23, 2015 01:53
-
-
Save Teino1978-Corp/a7bde01fbb581a707625 to your computer and use it in GitHub Desktop.
Sort array class
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 *) sortArray: (NSArray *) sortingArray withValuesInArray: (NSArray *) sorterArray{ | |
//We will create a NSDictionary using the second array as keys and the first array as values | |
NSDictionary *sortingDictionary = [NSDictionary dictionaryWithObjects:sorterArray forKeys:sortingArray]; | |
//Then we just create a new array sorted by the keys of the dictionary | |
NSArray *sortedArray = [sortingDictionary keysSortedByValueUsingComparator: ^(id value1, id value2) { | |
NSInteger number1 = [value1 integerValue]; | |
NSInteger number2 = [value2 integerValue]; | |
if (number1 > number2) { | |
return (NSComparisonResult)NSOrderedDescending; | |
} | |
if (number1 < number2) { | |
return (NSComparisonResult)NSOrderedAscending; | |
} | |
return (NSComparisonResult)NSOrderedSame; | |
}]; | |
return sortedArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment