Repository: realm/realm-swift
File: Realm/RLMQueryUtil.mm (Objective-C++)

Code before:
void RLMUpdateViewWithOrder(tightdb::TableView &view, id order, RLMObjectDescriptor *desc) {
    if (order) {
        NSString *propName;
        BOOL ascending = YES;
        
        // if not NSSortDescriptor or string then throw
        if ([order isKindOfClass:NSSortDescriptor.class]) {
            propName = [(NSSortDescriptor *)order key];
            ascending = [(NSSortDescriptor *)order ascending];
        }
        else if ([order isKindOfClass:NSString.class]) {
            propName = order;
        }
        else {
            @throw [NSException exceptionWithName:@"RLMException"
                                           reason:@"Invalid object for order - must use property name or NSSortDescriptor"
                                         userInfo:nil];
        }
        
        // validate
        RLMProperty *prop = desc[propName];
        if (!prop) {
            @throw RLMPredicateException(@"Invalid sort column",
                                         [NSString stringWithFormat:@"Column named '%@' not found.", propName]);
        }
        if (prop.type != RLMPropertyTypeInt && prop.type != RLMPropertyTypeBool && prop.type != RLMPropertyTypeDate) {
            @throw RLMPredicateException(@"Invalid sort column type",
                                         @"Sort only supported on Integer, Date and Boolean columns.");
        }
        view.sort(prop.column, ascending);
    }
}



Review comment (suggestion):
Microoptimization; Maybe the check on date should be before bool as I believe it is more likely that date is used than bool.


Code after:
void RLMUpdateViewWithOrder(tightdb::TableView &view, id order, RLMObjectDescriptor *desc) {
    if (order) {
        NSString *propName;
        BOOL ascending = YES;
        
        // if not NSSortDescriptor or string then throw
        if ([order isKindOfClass:NSSortDescriptor.class]) {
            propName = [(NSSortDescriptor *)order key];
            ascending = [(NSSortDescriptor *)order ascending];
        }
        else if ([order isKindOfClass:NSString.class]) {
            propName = order;
        }
        else {
            @throw [NSException exceptionWithName:@"RLMException"
                                           reason:@"Invalid object for order - must use property name or NSSortDescriptor"
                                         userInfo:nil];
        }
        
        // validate
        RLMProperty *prop = desc[propName];
        if (!prop) {
            @throw RLMPredicateException(@"Invalid sort column",
                                         [NSString stringWithFormat:@"Column named '%@' not found.", propName]);
        }
        if (prop.type != RLMPropertyTypeInt && prop.type != RLMPropertyTypeBool && prop.type != RLMPropertyTypeDate
            && prop.type != RLMPropertyTypeDouble && prop.type != RLMPropertyTypeFloat && prop.type != RLMPropertyTypeString) {
            @throw RLMPredicateException(@"Invalid sort column type",
                                         @"Sort only supported on Integer, Float, Double, Date, String and Boolean columns.");
        }
        view.sort(prop.column, ascending);
    }
}