Skip to content

Instantly share code, notes, and snippets.

@Teino1978-Corp
Created November 23, 2015 01:53
Show Gist options
  • Save Teino1978-Corp/a7bde01fbb581a707625 to your computer and use it in GitHub Desktop.
Save Teino1978-Corp/a7bde01fbb581a707625 to your computer and use it in GitHub Desktop.
Sort array class
+ (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