-1

I have an array of NSString objects, each NSString includes characters which have different priorities. Example: "abc13#$#kポ", this string has a number, a character, a specific character, and a Japanese character. Assume I set priority when sort according to order, 1, 2, 3, 4 for the numbers, character, specific, Japanse alternatively. How would I do that in this situation? Priority when sort array is: Latin->number->sybol->japanse character. Each item(ex: Latin) has oder according to unicode.

Thanks in advanced.

LViet
  • 113
  • 11
  • 2
    If plain vanilla sort doesn't do it for you use one of the `sortedArrayUsingXXX` functions. Which XXX you choose is up to you. – Hot Licks Jul 15 '14 at 15:21
  • you can define a category on NSString and use sortedArrayUsingSelector: or a more modern way would be with a block – Grady Player Jul 15 '14 at 15:24
  • possible duplicate of [How to sort an NSMutableArray with custom objects in it?](http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it) – Grady Player Jul 15 '14 at 15:26
  • 1
    I think if performance is a consideration then storing the string along with some decomposed data (that you would normally do in your sort method) would save a lot of time. – trojanfoe Jul 15 '14 at 15:26

2 Answers2

0

First find out precisely what you want. You can't sort the way you want to sort until you figured out exactly what you want.

To sort an NSMutableArray of strings, you call the sortUsingComparator method and pass in a comparator that compares the strings, any way you like. You write the code.

NSString has built-in methods that are quite flexible, for example handling numbers so that x9, x10, x99, x100 will be sorted in that order.

gnasher729
  • 47,695
  • 5
  • 65
  • 91
0

Store character as string in array and then sort the same accordingly:-

  NSMutableArray *yourArr=[NSMutableArray array];
  NSString *inputString = @"abc13";
  for (NSUInteger i = 0; i < inputString.length; i++)
 {
    NSString *character = [inputString substringWithRange:NSMakeRange(i, 1)];
    [yourArr addObject:character];
 }
   NSArray *sortedArray = [yourArr sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Hussain Shabbir
  • 13,963
  • 4
  • 34
  • 53