Τετάρτη, 7 Νοεμβρίου 2012

sort an array of objects with date property of them

finest solution i found was with block


sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSDate *first = [(MYOBJECT *)a date] ;
        NSDate *second = [(MYOBJECT *)b date];
        return [first compare:second];
    }];

to reverse ascending to deciding just reverse the first and second

sortedArray = [originalArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSDate *first = [(MYOBJECT *)a date] ;
        NSDate *second = [(MYOBJECT *)b date];
        return [second compare: first];
    }];

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου