开发者

how to sort array of strings

开发者 https://www.devze.com 2023-01-16 04:46 出处:网络
I create an array with string names as shown below NSMutableArray *strings = [[NSMutableArray alloc]init];

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

精彩评论

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