Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c sort NSMutableSet of NSStrings? [duplicate]

Possible Duplicate:
What is the most efficient way to sort an NSSet?

I currently have an NSMutableSet with the following a list of strings. e.g.: {@"c",@"d",@"e",@"f",@"g",@"a"}

Can someone please tell me how I can sort these values alphabetically? Is there a way to do this using sortedArrayUsingDescriptors: ?

like image 203
dpigera Avatar asked Dec 22 '11 19:12

dpigera


2 Answers

Sure, you can use that method:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES];
NSArray *sortedArray = [mySet sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

This just creates a new sort descriptor that specifies the key "description" (NSString provides a -description method that returns the string). It then passes an array containing that sort descriptor as a parameter to -sortedArrayUsingDescriptors:, which you can send to any NSSet.

like image 68
Caleb Avatar answered Sep 23 '22 19:09

Caleb


I'm going to assume that this question Sorting NSSet is the answer to your very question. For the hard of clicking ...

[mySet sortedArrayUsingDescriptors:descriptors];
like image 41
NicholasTGD Avatar answered Sep 23 '22 19:09

NicholasTGD