Christian Gossain
Christian Gossain

Reputation: 5972

Determining if a day of the week lies in between two others in an NSMutableArray?

If I have an array that holds various days of the week (i.e. @"Fri, Mar 11, 2011", @"Wed, Mar 9, 2011", @"Tue, Mar 8, 2011", @"Mon, Mar 7, 2011") and this array is constantly changing because users are adding/removing days from the array, and let's say that I create some variable NSString *myDay = @"Thu, Mar 10, 2011"; how can I programatically determine that specifically between the first and second elements in the array (i.e. in this case @"Fri, Mar 11, 2011" and @"Wed, Mar 9, 2011") that myDay(i.e. @"Thu, Mar 10, 2011") does not lies in between them (or does in other cases)?

Note: In this array there can be multiple entries for a specific day of the week (i.e Thursday), it will just be part of a different week.

Upvotes: 1

Views: 114

Answers (2)

Tim
Tim

Reputation: 60130

If I understand your question right, you're only looking to deal with the contents of the array, not with the true concept of what days are between other days. In that case, you can use a couple calls to indexOfObject:, then compare, to see whether your object is between them. For example:

- (BOOL)array:(NSArray *)arr hasObject:(id)obj between:(id)a and:(id)b {
    NSUInteger startIdx = [arr indexOfObject:a];
    NSUInteger endIdx = [arr indexOfObject:b];
    NSUInteger targetIdx = [arr indexOfObject:obj];

    if(startIdx != NSNotFound && endIdx != NSNotFound && targetIdx != NSNotFound 
                && startIdx <= targetIdx && targetIdx <= endIdx) {
        return YES;
    } else {
        return NO;
    }
}

NSArray * days = [NSArray arrayWithObjects:@"Mon", @"Wed", @"Fri", @"Sat", nil];
[self array:days hasObject:@"Wed" between:@"Mon" and:@"Fri"]; // YES
[self array:days hasObject:@"Wed" between:@"Wed" and:@"Wed"]; // YES
[self array:days hasObject:@"Sat" between:@"Mon" and:@"Fri"]; // NO
[self array:days hasObject:@"Thu" between:@"Mon" and:@"Fri"]; // NO
[self array:days hasObject:@"Wed" between:@"Fri" and:@"Mon"]; // NO

In this example, we don't care about what the objects actually represent (days of the week), so even though Thursday is between Monday and Friday, the method returns NO because Thursday doesn't exist in the array.

The second example call is also an interesting case - you can easily modify the method to use strict inequality when comparing, so that an object is not "between" two others if it matches one (or both) of the others.

Upvotes: 1

Jim
Jim

Reputation: 3294

Depending on what you actually want to do you could do something like (not sure if this compiles 100%). You can adapt the below depending on what you really want to find out and return. Also remember to release stuff after that you don't want to retain.

NSString *token = @"Wed";

NSMutableArray *before = [NSMutableArray init];
NSMutableArray *after = [NSMutableArray init];

bool beforeToken = true;

for(id item in yourArray) {
   if([token isEqualToString:item]) {
       beforeToken = false;
       continue;
   }

   if(beforeToken)
       [before addObject:item];
   else
       [after addObject:item];

}

Upvotes: 0

Related Questions