1

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

MobX
  • 2,498
  • 6
  • 30
  • 49

2 Answers2

1

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

NSArray *newArray = [oldArray sortedArrayUsingSelector:@selector(compare:)];
deanWombourne
  • 37,003
  • 13
  • 93
  • 99
0

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: How to sort an NSMutableArray with custom objects in it?

Community
  • 1
  • 1
Ben
  • 2,982
  • 1
  • 14
  • 12