-2

I have object Item and it has only two NSDate properties.
From those properties only important part for me is time.
If it is possible this doesn't have to be NSDate it can be NSString with just a @"14:30" and @"17:30".
I have 10 objects with different times and they are in NSMutableArray.
I need to sort all those objects to load them in table.
I have tried something but no luck (actually this code works for one property) with two properties:

NSDate *now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
    [components setHour:12];
    NSDate *todayA = [calendar dateFromComponents:components];
    [components setHour:11];
    NSDate *todayB = [calendar dateFromComponents:components];
    [components setHour:10];
    NSDate *todayC = [calendar dateFromComponents:components];

    Item* i1 = [[Item alloc] init];
    i1.DateTimeA = todayA;
    Item* i2 = [[Item alloc] init];
    i2.DateTimeA = todayB;
    Item* i3 = [[Item alloc] init];
    i3.DateTimeA = todayC;
    [array addObject:i1];
    [array addObject:i2];
    [array addObject:i3];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"DateTimeA" ascending:NO];
    NSArray *orderedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

As I said Item DateTimeA and DateTimeB doesn't have to be NSDate they can be only strings but I must be able to sort by those two properties.
Is there any solution for this?

1110
  • 7,829
  • 55
  • 176
  • 334
  • If I understand correctly you want to sort only by time no matter which days the dates are from ? – Ayu Aug 07 '14 at 11:43
  • Yes sir. Only by time. As I said it can be and string with time only. – 1110 Aug 07 '14 at 11:46
  • If you had bothered to read the NSArray spec you would have seen several other "sortedArrayUsing..." methods. – Hot Licks Aug 07 '14 at 12:06

2 Answers2

1

Try that:

array = [array sortedArrayUsingComparator:^(Item *i1, Item *i2) {

                             NSDateFormatter* df1 = [[NSDateFormatter alloc] init];
                             [df1 setDateFormat:@"d/LLL/yy"]; // <-- make sure it match your date format
                             NSDate* date1 = [df1 dateFromString:i1.DateTimeA];
                             NSDate* date2 = [df1 dateFromString:i2.DateTimeA];
                             return [date2 compare:date1];
                         }];

This is just example but you can customize it to match your requirements. In this example I assume that DateTimeA is always NSString but you should cheek it first and if it's NSDate you can omit formatting bit.

For example:

if ([i1.DateTimeA isKindOfClass:[NSDate class]])
    NSDate* date1 = i1.DateTimeA;
if ([i2.DateTimeA isKindOfClass:[NSDate class]])
    NSDate* date2 = i2.DateTimeA;

return [date2 compare:date1];
Greg
  • 25,317
  • 6
  • 53
  • 62
1

This should the trick

NSArray *sortedArray = [array sortedArrayUsingComparator:^(Item *i1, Item *i2) {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"HHmmss"];
            NSDate* date1A = [dateFormatter dateFromString:[dateFormatter stringFromDate:i1.DateTimeA]];
            NSDate* date2A = [dateFormatter dateFromString:[dateFormatter stringFromDate:i2.DateTimeA]];
            NSDate* date1B = [dateFormatter dateFromString:[dateFormatter stringFromDate:i1.DateTimeB]];
            NSDate* date2B = [dateFormatter dateFromString:[dateFormatter stringFromDate:i2.DateTimeB]];
            switch ([date1A compare:date2A]) {
                case NSOrderedSame:
                    return [date1B compare:date2B];
                    break;

                default:
                    return [date1A compare:date2A];
                    break;
            }
        }];

It's not very pretty but it'll do what you want, note that you won't need to this if you have let's say DateTimeA and DateTimeB stored as string in a format like HH:mm or whatever, you'll need only to do:

[dateFormatter dateFromString:DateTimeA];

One more thing avoid starting your iVars and properties names with an upper case letter that's really confusing.

Hope this helps.

Ayu
  • 926
  • 1
  • 9
  • 15