2

I have an array: Array A which contains the objects

"Anchorage, AK"
"Juneau, AK"
"Los Angeles, CA"
"Minneapolis, MS"
"Seatac, WA"
"Seattle, WA"

Note: actual array objects do not contain quotation marks.

How can I separate this array into multiple arrays based on the last to characters of the string? It does not matter if the arrays are mutable or not to me.

i.e...

Array 2 {
[1] <--- First NSArray
"Anchorage, AK"
"Jeneau, AK"

[2] <--- Second NSArray
"Los Angeles, CA"

[3] <--- Third NSArray
"Minneapolis, MS"

[4] <--- Fourth NSArray
"Seatac, WA"
"Seattle, WA"
}

In the real scenario I will not know how many of each state there are. I'm thinking I can do something with the two char length part of the string at the end? Because thats what I want them separated into essentially is states.

Milo
  • 5,041
  • 7
  • 33
  • 59
  • Try to implement something and post the non-working code. We can't just write it for you. – Fabien Warniez Jan 21 '14 at 01:33
  • What are the criteria by which you want to split the array? – jscs Jan 21 '14 at 01:38
  • @JoshCaswell I want to group the objects based on the last two characters in the string objects. – Milo Jan 21 '14 at 01:42
  • 2
    Why do you want an array of arrays? You should have a dictionary where the keys are the states and the values are arrays of cities for the state. – rmaddy Jan 21 '14 at 02:06

2 Answers2

3

Okay - let me know if this is clear.

// Setup the inital array
NSArray *array = [[NSArray alloc] initWithObjects:@"Anchorage, AK",
                  @"Juneau, AK",
                  @"Los Angeles, CA",
                  @"Minneapolis, MS",
                  @"Seatac, WA",
                  @"Seattle, WA", nil];

// Create our array of arrays
NSMutableArray *newArray2 = [[NSMutableArray alloc] init];

// Loop through all of the cities using a for loop
for (NSString *city in array) {
    // Keep track of if we need to creat a new array or not
    bool foundCity = NO;

    // This gets the state, by getting the substring of the last two letters
    NSString *state = [city substringFromIndex:[city length] -2];

    // Now loop though our array of arrays tosee if we already have this state
    for (NSMutableArray *subArray in newArray2) {
        //Only check the first value, since all the values will be the same state
        NSString *arrayCity = (NSString *)subArray[0];
        NSString *arrayState = [arrayCity substringFromIndex:[arrayCity length] -2];

        if ([state isEqualToString:arrayState])
        {
            // Check if the states match... if they do, then add it to this array
            foundCity = YES;
            [subArray addObject:city];

            // No need to continue the for loop, so break stops looking though the arrays.
            break;
        }
    }

    // WE did not find the state in the newArray2, so create a new one
    if (foundCity == NO)
    {
        NSMutableArray *newCityArray = [[NSMutableArray alloc] initWithObjects:city, nil];
        [newArray2 addObject:newCityArray];
    }


}

//Print the results
NSLog(@"%@", newArray2);

My output

2014-01-20 20:28:04.787 TemperatureConverter[91245:a0b] (
        (
        "Anchorage, AK",
        "Juneau, AK"
    ),
        (
        "Los Angeles, CA"
    ),
        (
        "Minneapolis, MS"
    ),
        (
        "Seatac, WA",
        "Seattle, WA"
    )
)
ansible
  • 3,569
  • 2
  • 18
  • 29
0

You can loop through the strings in your original array and split them by delimiters and put them into new array. Then you can look at the arrays and group based on the second element on the array.

Community
  • 1
  • 1
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
  • Can you elaborate on how to group them based on the second element? I have an array with all the two char state names in the above array. – Milo Jan 21 '14 at 01:40