Created
November 9, 2015 22:21
-
-
Save garmstro/1c752f1c214bdfe7ccc8 to your computer and use it in GitHub Desktop.
NSArray Extensions
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+NSArrayExtensions.h | |
// ArrayFunctions | |
// | |
// Created by Geoff Armstrong on 11/4/15. | |
// Copyright © 2015 Geoff Armstrong. All rights reserved. | |
// | |
#ifndef NSArray_NSArrayExtensions_h | |
#define NSArray_NSArrayExtensions_h | |
@interface NSArray (NSArrayExtensions) | |
/** | |
Rotates an array by index elements | |
@param index The index to rotate the array to | |
@return A rotated array | |
*/ | |
- (instancetype) rotateArrayToIndex: (int) index; | |
/** | |
Sorts an array of NSString alphabetically using optimized bubble sort | |
@return The sorted array | |
*/ | |
- (instancetype) sortStringArray; | |
/** | |
Sorts an array of NSNumber in increasing order using optimized bubble sort | |
@return The sorted array | |
*/ | |
- (instancetype) sortNumberArray; | |
@end | |
#endif /* NSArray_NSArrayExtensions_h */ |
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+NSArrayExtensions.m | |
// ArrayFunctions | |
// | |
// Created by Geoff Armstrong on 11/4/15. | |
// Copyright © 2015 Geoff Armstrong. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "NSArray+NSArrayExtensions.h" | |
@implementation NSArray (NSArrayExtensions) | |
- (instancetype) rotateArrayToIndex:(int)n { | |
NSMutableArray *newArray = [NSMutableArray arrayWithArray:self]; | |
for (int i=0; i < n; i++){ | |
NSObject *theObject = [newArray objectAtIndex:0]; | |
[newArray removeObjectAtIndex:0]; | |
[newArray addObject:theObject]; | |
} | |
return newArray; | |
} | |
- (instancetype) sortStringArray { | |
NSMutableArray *sortedArray = [NSMutableArray arrayWithArray: self]; | |
//Keep track of how many loops have been made, the last item is not required for each subsequent pass. | |
int loopCount = 0; | |
//Initialize didSwap to YES to step into the loop | |
BOOL didSwap = YES; | |
//Continue until no changes are made to the array | |
while (didSwap) { | |
didSwap = NO; | |
for ( int i=1; i < sortedArray.count - loopCount; i++ ) { | |
NSString *stringA = [sortedArray objectAtIndex:i - 1]; | |
NSString *stringB = [sortedArray objectAtIndex:i]; | |
unichar characterA = [stringA characterAtIndex:0]; | |
unichar characterB = [stringB characterAtIndex:0]; | |
int charStep = 1; | |
while (characterA == characterB) { | |
if (charStep < [stringA length]) { | |
characterA = [stringA characterAtIndex:charStep]; | |
} | |
if (charStep < [stringB length]) { | |
characterB = [stringB characterAtIndex:charStep]; | |
} | |
//If we have reached the end of both strings, they are the same. | |
if (charStep >= stringA.length && charStep >= stringB.length) { | |
break; | |
} | |
charStep++; | |
} | |
//If the character code for A is greater than the character code for B then swap the two items | |
if (characterA > characterB) { | |
[sortedArray removeObjectAtIndex:i]; | |
[sortedArray insertObject:stringB atIndex:i - 1]; | |
didSwap = YES; | |
} | |
} | |
loopCount++; | |
} | |
return sortedArray; | |
} | |
- (instancetype) sortNumberArray { | |
NSMutableArray *sortedArray = [NSMutableArray arrayWithArray: self]; | |
//Keep track of how many loops have been made, the last item is not required for each subsequent pass. | |
int loopCount = 0; | |
//Initialize didSwap to YES to step into the loop | |
BOOL didSwap = YES; | |
//Continue until no changes are made to the array | |
while (didSwap) { | |
didSwap = NO; | |
for ( int i=1; i < sortedArray.count - loopCount; i++ ) { | |
NSNumber *numberA = [sortedArray objectAtIndex:i - 1]; | |
NSNumber *numberB = [sortedArray objectAtIndex:i]; | |
//If the number for A is greater than the number for B then swap the two items | |
if ([numberA isGreaterThan:numberB]) { | |
[sortedArray removeObjectAtIndex:i]; | |
[sortedArray insertObject:numberB atIndex:i - 1]; | |
didSwap = YES; | |
} | |
} | |
loopCount++; | |
} | |
return sortedArray; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment