tags:

views:

74

answers:

2

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 button the strings are exchanged how can I do this?

+2  A: 

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:

vodkhang
A: 

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:)].

Toro
[strings sortedArrayUsingSelector:@selector(compare:)] i am try using this but strings are not exchanged.
MaheshBabu
Does your string look really like the above? Is it localized to some specific languages (e.g Chinese, Japanese)?
vodkhang
the method works fine, but you are adding the strings to the NSArray in an already sorted order.try putting "string4" at first position then run.
vaitrafra
try `strings = [strings sortedArrayUsingSelector:@selector(compare:)];` as it doesn't work in-place.
vikingosegundo