oscarimonbox
3/18/2016 - 8:59 AM

Ordenar array

Ordenar array

let ordered = imageArray.sorted(by: {$0.position < $1.position})
    func orderTimetable(array: [Availability]) -> [Availability] {
        var ordered = [Availability]()
        
        ordered = array.sorted(by: {
            
            $0.time?.compare($1.time ?? "", options: .numeric) == .orderedAscending
            
        })
        
        return ordered
    }
NSSortDescriptor* sortByDate = [NSSortDescriptor sortDescriptorWithKey:@"nsdatepropertyname" ascending:YES];

    [mutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortByDate]];
    
    
o si es un campo objeto custom

NSSortDescriptor (better)
or usually even better:

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                              ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];
You can easily sort by multiple keys by adding more than one to the array. Using custom comparator-methods is possible as well. Have a look at the documentation.

Blocks (shiny!)
There's also the possibility of sorting with a block since Mac OS X 10.6 and iOS 4:

NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSDate *first = [(Person*)a birthDate];
    NSDate *second = [(Person*)b birthDate];
    return [first compare:second];
}];