0

Initial Array

{
    "Golf > Short game > Ballflight / Target",
    "Mental > I do (behavior/skills - how) > Energy / Emotions",
    "Fitness > Endurance",
    "Fitness > Flexibility",
    "Golf > Long game",
    "Golf > Long game > Approach from fairway",
    "Golf > Practice Game",
}

I want to sort the above array as starting from golf , fitness and mental.
so Resulting array is like as below

{
    "Golf > Short game > Ballflight / Target",
    "Golf > Long game",
    "Golf > Long game > Approach from fairway",
    "Golf > Practice Game",
    "Fitness > Endurance",
    "Fitness > Flexibility",
    "Mental > I do (behavior/skills - how) > Energy / Emotions",
}

Please guide me.

I have try using for loop but i want some simple solution to parse it.

Thanks .

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Haresh Ghatala
  • 1,996
  • 17
  • 25

2 Answers2

1

Example code (sort based on the first word of the line):

NSMutableArray *myArray = [@[
    @"Golf > Short game > Ballflight / Target",
    @"Mental > I do (behavior/skills - how) > Energy / Emotions",
    @"Fitness > Endurance",
    @"Fitness > Flexibility",
    @"Golf > Long game",
    @"Golf > Long game > Approach from fairway",
    @"Golf > Practice Game",
    ] mutableCopy];

NSDictionary *scores = @{@"Golf":@1, @"Fitness":@2, @"Mental":@3};

[myArray sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {

    // first word (can be refined checking for better word break chars)
    NSString *prefix1;
    NSRange rangeUntilSpace1 = [str1 rangeOfString:@" "];
    if (rangeUntilSpace1.location != NSNotFound)
        prefix1 = [str1 substringToIndex:rangeUntilSpace1.location];
    else
        prefix1 = str1;

    NSString *prefix2;
    NSRange rangeUntilSpace2 = [str2 rangeOfString:@" "];
    if (rangeUntilSpace2.location != NSNotFound)
        prefix2 = [str2 substringToIndex:rangeUntilSpace2.location];
    else
        prefix2 = str2;

    // scores (taken from the previous dictionary)
    NSInteger score1 = [scores[prefix1] intValue];
    NSInteger score2 = [scores[prefix2] intValue];

    if (score1 && score2) {

        return score1 > score2 ? NSOrderedDescending : NSOrderedAscending;
    } else if (score1) {

        return NSOrderedAscending;  // if not in scores dictionary, put down
    } else {

        return NSOrderedDescending; // if not in scores dictionary, put down
    }

}];
valfer
  • 3,545
  • 2
  • 19
  • 24
0

You can use - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr

Here is an example.

NSArray *array = @[@"G", @"M", @"F"] ;
array = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSString *s1 = obj1 ;
    NSString *s2 = obj2 ;
    unichar u1 = [s1 characterAtIndex:0] ;
    unichar u2 = [s2 characterAtIndex:0] ;
    if (u1 == u2) {
        return [s1 compare:s2] ;
    } else {
        if (u1 == 'G') {
            return NSOrderedAscending ;
        } else if (u1 == 'M') {
            return NSOrderedDescending ;
        } else {
            return u2 == 'G' ? NSOrderedDescending : NSOrderedAscending ;
        }
    }
}] ;
NSLog(@"%@", array) ;
KudoCC
  • 6,912
  • 1
  • 24
  • 53