开发者

how to sort array of strings

I create an array with string names as shown below

NSMutableArray *strings = [[NSMutableArray alloc]init];
[string addObject:@"string1"];
[string addObject:@"string2"];
[string addObject:@"string3"];
[string addObject:@"string4"];

and I create a button. Whenever I click the but开发者_开发百科ton the strings are exchanged how can I do this?


EDIT:

Looks like you do not really lack basic knowledge. You can call this method in NSArray after you add your objects:

This method is the simplest way to do your job:

NSArray *sortedStrings = [strings sortedArrayUsingSelector:@selector(compare:)];

More about sortedArrayUsingSelector:


You can see NSArray class reference about following methods.

Sorting

  • sortedArrayHint
  • sortedArrayUsingFunction:context:
  • sortedArrayUsingFunction:context:hint:
  • sortedArrayUsingDescriptors:
  • sortedArrayUsingSelector:
  • sortedArrayUsingComparator:
  • sortedArrayWithOptions:usingComparator:

As for your problem, you can sort strings by
[strings sortedArrayUsingSelector:@selector(compare:)].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜