tags:

views:

17

answers:

2

Hi , I need to order a set of strings in an array to some predefined order , like,If my array contains field [a,b,c,d] then i need to order the elements to a predefined order [c,a,b,d].If the source array miss any element then also i need to order it in predefined manner like if the source array contains [a,c,d] then i need the result as [c,a,d],Is this possible using NSPredicate in iPhone SDK.

Thanks in advance

A: 

You can sort your array with an NSSortDescriptor like:

NSSortDescriptor *order = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:YES];
NSArray *sorts = [[NSArray alloc] initWithObjects:order,nil];

NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sorts];
[sorts release];
[order release];

You create an array of sort descriptors which are applied in order.

You could also sort using a selector, see this answer for more options: http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it

Ben
A: 

I'd just use NSString's built in compare: method :

NSArray *newArray = [oldArray sortedArrayUsingSelector:@selector(compare:)];
deanWombourne