Sorting an NSArray of NSString

26,460

Solution 1

It's pretty simple to write your own comparison method for strings:

@implementation NSString(compare)

-(NSComparisonResult)compareNumberStrings:(NSString *)str {
    NSNumber * me = [NSNumber numberWithInt:[self intValue]];
    NSNumber * you = [NSNumber numberWithInt:[str intValue]];

    return [you compare:me];
}

@end

Solution 2

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

Solution 3

If the array's elements are just NSStrings with digits and no letters (i.e. "8", "25", "3", etc.), here's a clean and short way that actually works:

NSArray *sortedArray = [unorderedArray sortedArrayUsingComparator:^(id a, id b) {
    return [a compare:b options:NSNumericSearch];
}];

Done! No need to write a whole method that returns NSComparisonResult, or eight lines of NSSortDescriptor...

Solution 4

IMHO, easiest way to sort such array is

[arr sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"self.intValue" ascending:YES]]]

If your array contains float values, just change key to self.floatValue or self.doubleValue

Solution 5

The easiest way would be to make a comparator method like this one

NSArray *sortedStrings = [stringsArray sortedArrayUsingComparator:^NSComparisonResult(NSString *firstString, NSString *secondString) {
    return [[firstString lowercaseString] compare:[secondString lowercaseString]];
}];
Share:
26,460
CodeGuy
Author by

CodeGuy

Updated on November 03, 2020

Comments

  • CodeGuy
    CodeGuy over 3 years

    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!

  • CodeGuy
    CodeGuy over 13 years
    I tried that second way and here is the output of a sorted array:
  • CodeGuy
    CodeGuy over 13 years
    2, 2, 2, 2, 3, 3, 32, 4, 47, 477, 56, 7, 8
  • HyLian
    HyLian over 13 years
    You need to replace the comparator given in the example, i didn't know what was you sorting "criteria" :)
  • Martin
    Martin about 11 years
    You need to declare a & b as id otherwise Xcode will produce an error : [unorderedArray sortedArrayUsingComparator:^(id a, id b) {...}];
  • ReDetection
    ReDetection over 8 years
    this won't help when you have numbers in string (as in question)
  • damianostre
    damianostre about 7 years
    It also works with strings that contain non-digits, e.g. "File 1", "File 2", etc. - it will then correctly sort "File 2" before "File 12".