0

I have an array of dates:

"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-11 23:00:00 +0000",
"2014-05-11 23:00:00 +0000"

And i want to split this array to two different, and put in one array,

Result of what i want:

(
    "2014-05-09 23:00:00 +0000",
    "2014-05-09 23:00:00 +0000",
    "2014-05-09 23:00:00 +0000",
    "2014-05-09 23:00:00 +0000",
    "2014-05-09 23:00:00 +0000",
    "2014-05-09 23:00:00 +0000",

),
    (
    "2014-05-11 23:00:00 +0000",
    "2014-05-11 23:00:00 +0000"
)

Here's what i've tried:

self.tempDatesArray - array of dates (unique)
tempArray - array of all dates

self.tempDatesArray = [[[NSOrderedSet orderedSetWithArray:[self.tempDatesArray copy]] array] mutableCopy]; // i'm getting all the unique dates from array

if (self.tempDatesArray.count > 0){
                for (int j=0;j<self.tempDatesArray.count;j++){
                    if (tempArray[i] == self.tempDatesArray[j]){

                        [tempArray2 addObject:tempArray[i]]; // temp array

                        if (j++){

                            NSLog(@"i is %i",i);

                            [resultArray addObject:tempArray2];

                            NSLog(@"test %@",self.sectionsArray);

                            tempArray2 = [@[] mutableCopy];
                            }
                        }
                    }
            }

And my result is :

(
        "2014-05-09 23:00:00 +0000",
        "2014-05-09 23:00:00 +0000",
        "2014-05-09 23:00:00 +0000",
        "2014-05-09 23:00:00 +0000",
        "2014-05-09 23:00:00 +0000",
        "2014-05-09 23:00:00 +0000",
        "2014-05-11 23:00:00 +0000"
    ),
        (
        "2014-05-11 23:00:00 +0000"
    )

I'm little frustrated because of this, can some one put me in right direction?

2
  • You need to state the criteria you're using to separate dates. Do you want all the duplicates in 1 array and the rest in another array? As the other poster said, NSDate is really a wrapper around a double precision floating point number, so dates that appear the same may be off by a small fraction of a second and hence not compare as equal. Commented May 11, 2014 at 19:49
  • Or are you looking to separate your dates by day? In that case you might want to use NSCalendar and NSDateComponents to get the ordinal day value for each date and compare them. Commented May 11, 2014 at 19:52

4 Answers 4

2

Yet another approach:

NSMutableArray *result = [NSMutableArray new];
[[arrayOfDates valueForKeyPath:@"@distinctUnionOfObjects.self"] enumerateObjectsUsingBlock:^(NSDate *date, NSUInteger idx, BOOL *stop) {

    NSIndexSet *indexSet = [arrayOfDates indexesOfObjectsPassingTest:^BOOL(NSDate *d, NSUInteger idx, BOOL *stop) {
        return [d isEqual:date];
    }];

    [result addObject:[arrayOfDates objectsAtIndexes:indexSet]];
}];
Sign up to request clarification or add additional context in comments.

3 Comments

What sorcery is this? Can you point me towards the docs mentioning this "distinctUnionOfObjects.self" and possibly other witchcraft?
Well, I know what I'm doing today! I didn't know you could use keyValue stuff like this, thanks for the great info! This is why I come to StackOverflow . . . the things I had no idea I didn't know.
1

I think something like this should work:

NSArray * allDates = @[...];

NSSet * uniqueDates = [NSSet setWithArray:allDates];

NSMutableArray * groupedDatesArray = [NSMutableArray array];
for (NSDate * uniqueDate in uniqueDates) {
    NSMutableArray * sharedDateArray = [NSMutableArray array];
    for (NSDate * newDate in allDates) {
        if ([newDate isEqualToDate:uniqueDate]) {
            [sharedDateArray addObject:uniqueDate];
        }
    }
    [groupedDatesArray addObject:sharedDateArray];
}

NSLog(@"GroupedDates: %@", groupedDatesArray);

1 Comment

whoa, you're a life saver
1
NSSet *set = [NSSet setWithArray:self.tempDateArray];
NSMutableArray  *groupedArray = [NSMutableArray array];

for (NSDate *date in [set allObjects]) {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF = %@", date];
    NSArray *matchingArray = [self.tempDateArray filteredArrayUsingPredicate:predicate];
    [groupedArray addObject:matchingArray];
}

Comments

0

First, you say that you are looking for unique objects, but then in your expected results you have lots of duplicates. What are you really trying to do here?

I suspect that your second 2014-05-11 23:00:00 +0000 is getting lost because putting it into the ordered set is coalescing those into one entry. When the second 05-11 entry is added to the set, the set says "I already have that: it isn't unique".

Why aren't all of the duplicate 2014-05-09 23:00:00 entries getting coalesced then? I'm not sure. I'd have to see more of the code. Perhaps floating point issues. After all, NSDates are essentially big wrappers around floating points since a reference date. And comparing floating points is a fool's errand.

There are other things that confuse me about your code, but before I invest time trying to figure out what and why you are doing, do you really want to remove your duplicates? If so, what tolerance do you have between duplicates? Are 23:00:00 and 23:00:00.1 the same? 23:00:00.0001?

Or are you just trying to break down your arrays into subarrays based on day?

1 Comment

it's simply your last sentence there, David, the OP is after

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.