Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can someone please show me the code for sorting an NSMutableArray? I have the following NSMutableArray:

NSMutableArray *arr = [[NSMutableArray alloc] init];

with elements such as "2", "4", "5", "1", "9", etc which are all NSString.

I'd like to sort the list in descending order so that the largest valued integer is highest in the list (index 0).

I tried the following:

[arr sortUsingSelector:@selector(compare:)];

but it did not seem to sort my values properly.

Can someone show me code for properly doing what I am trying to accomplish? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
517 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should use this method:

[arr sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

in a NSArray or:

[arr sortUsingSelector:@selector(caseInsensitiveCompare:)];

in a "inplace" sorting NSMutableArray

The comparator should return one of this values:

  • NSOrderedAscending
  • NSOrderedDescending
  • NSOrderedSame

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...