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.

4
  • Try to implement something and post the non-working code. We can't just write it for you. Commented Jan 21, 2014 at 1:33
  • What are the criteria by which you want to split the array? Commented Jan 21, 2014 at 1:38
  • @JoshCaswell I want to group the objects based on the last two characters in the string objects. Commented Jan 21, 2014 at 1: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. Commented Jan 21, 2014 at 2:06

2 Answers 2

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"
    )
)
Sign up to request clarification or add additional context in comments.

6 Comments

But what do I do if I don't know how many of each state there are?
I don't understand your goal, how exactly do you want to break up the array? What algorithm are you following? First and last array is 2 and the rest are one?
I've clarified my question
Why does Anchorage and Seatac repeat twice?
Because I had [subArray addObject:arrayCity]; instead of [subArray addObject:city]; - fixed.
|
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.