0

I would like to sort an array of custom objects based on the name(NSString) variable. The name variable can have numbers in the end.

Unsorted data: Tank 1, Tank 2, Tank 10, Tank 32, Tank 3, Tank 11

I used the following sort descriptor,

[NSSortDescriptor sortDescriptorWithKey:@"objectName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]

The resultant array gives: Tank 1, Tank 10, Tank 11, Tank 2, Tank 3, Tank32

But I need to get the array based on the actual numbers also, I mean the resultant array should be: Tank 1, Tank 2, Tank 3, Tank 10, Tank 11, Tank 32.

What change needs to be made to the sort descriptor to accomplish this?. Could someone help me on this.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

You might want to look at the sortedArrayUsingComparator: method if NSArray. You provide a block used to compare and sort the items in your array. In your case you might extract the number at the end of each string and compare those.

Alex
  • 1,625
  • 12
  • 18