Sort characters in NSString in alphabetical order

I am trying to redo the words in alphabetical order. For example, a tomato would become amoott, or the stack would become ackst.

I found several methods for doing this in C with char arrays, but I am having problems working within an NSString object.

Is there an easier way to do this inside the NSString object itself?

+6
source share
3 answers

I think that highlighting a string into an array of strings (each row in the array contains only one char from the original string). Then array sorting will be OK. This is inefficient, but enough when the string is not very long. I checked the code.

NSString *str = @"stack"; NSMutableArray *charArray = [NSMutableArray arrayWithCapacity:str.length]; for (int i=0; i<str.length; ++i) { NSString *charStr = [str substringWithRange:NSMakeRange(i, 1)]; [charArray addObject:charStr]; } NSString *sortedStr = [[charArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] componentsJoinedByString:@""]; 
+4
source

You can store each of the string characters in an NSArray objects, and then sort. It seems a bit expensive, so I could just use qsort() instead.

Here it is presented as an Objective-C category (untested):

NSString + SortExtension.h:

 #import <Foundation/Foundation.h> @interface NSString (SortExtension) - (NSString *)sorted; @end 

NSString + SortExtension.m:

 #import "NSString+SortExtension.h" @implementation NSString (SortExtension) - (NSString *)sorted { // init NSUInteger length = [self length]; unichar *chars = (unichar *)malloc(sizeof(unichar) * length); // extract [self getCharacters:chars range:NSMakeRange(0, length)]; // sort (for western alphabets only) qsort_b(chars, length, sizeof(unichar), ^(const void *l, const void *r) { unichar left = *(unichar *)l; unichar right = *(unichar *)r; return (int)(left - right); }); // recreate NSString *sorted = [NSString stringWithCharacters:chars length:length]; // clean-up free(chars); return sorted; } @end 
+10
source
 // --------- Function To Make an Array from String NSArray *makeArrayFromString(NSString *my_string) { NSMutableArray *array = [[NSMutableArray alloc] init]; for (int i = 0; i < my_string.length; i ++) { [array addObject:[NSString stringWithFormat:@"%c", [my_string characterAtIndex:i]]]; } return array; } // --------- Function To Sort Array NSArray *sortArrayAlphabetically(NSArray *my_array) { my_array= [my_array sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; return my_array; } // --------- Function Combine Array To Single String NSString *combineArrayIntoString(NSArray *my_array) { NSString * combinedString = [[my_array valueForKey:@"description"] componentsJoinedByString:@""]; return combinedString; } // Now you can call the functions as in below where string_to_arrange is your string NSArray *blowUpArray; blowUpArray = makeArrayFromString(string_to_arrange); blowUpArray = sortArrayAlphabetically(blowUpArray); NSString *arrayToString= combineArrayIntoString(blowUpArray); NSLog(@"arranged string = %@",arrayToString); 
+2
source

All Articles