StarvingPilot
StarvingPilot

Reputation: 27

Sorting UITableView results

SO here's my setup. I have an object called RadioStations where I have several strings like callsign, frequency declared and an NSMutableArray called amStationInfo. On my viewcontroller, I access an SQLite database which populates the an array like so...

RadioStations.h

@interface RadioStations : NSObject {

    NSString *format;
    NSString *city;
}

@property (nonatomic, retain) NSString *format;
@property (nonatomic, retain) NSString *city;

ViewController.m

NSMutableArray* amStationInfo = [[NSMutableArray alloc] init];

    while (sqlite3_step(statement) == SQLITE_ROW)
    {
        NSString *cityField = [[NSString alloc] initWithUTF8String:
                               (const char *) sqlite3_column_text(statement, 10)];
        NSString *formatField = [[NSString alloc] initWithUTF8String:
                                (const char *) sqlite3_column_text(statement, 0)];
        RadioStations *amStationClass = [[RadioStations alloc] init];            
                [amStationClass setCity:cityField];
                [amStationClass setFormat:formatField];
                [amStationInfo addObject:amStationClass];
    }
[tabView reloadData];
sqlite3_finalize(statement);

and then I populate a UITableView

NSString *cityValue = [(RadioStations *)[amStationInfo objectAtIndex:indexPath.row] city];
NSString *formatValue = [(RadioStations *)[amStationInfo objectAtIndex:indexPath.row] format];
cityLabel.text = cityValue;
formatLabel.text = formatValue;

I would like to be able to sort the UITable results based on city (alphabetical) or format (numeric) and both, city, and then format. I know format is NSString, let's just say I got the doubleValue of it already. I'd like to have results like so....

City (alphabetical) 1) Los Angeles - 5.0 2) San Francisco - 3.0 3) San Jose - 10.0 4) San Jose - 2.0 5) San Jose - 4.0

Format (numeric) 1) San Jose - 2.0 2) San Francisco - 3.0 3) San Jose - 4.0 4) Los Angeles - 5.0 5) San Jose - 10.0

Both (city then format) 1) Los Angeles - 5.0 2) San Francisco - 3.0 3) San Jose - 2.0 4) San Jose - 4.0 5) San Jose - 10

I have a method to sort the format like so... in RadioStations.m

- (NSComparisonResult)sortByFormat:(id)anObject
{
    if (self.format < anObject.format) {
        return NSOrderedAscending;
    } else if (self.format > anObject.format) {
        return NSOrderedDescending;
    }
    return NSOrderedSame;
}

but I get a "property 'format' not found on object of type '_strong id' and it was highlighting anObject.format

Upvotes: 0

Views: 106

Answers (1)

g_low
g_low

Reputation: 2455

Why aren't you passing in a RadioStation instead of an id? Maybe I am missing something, but I think that is all you need to do...

    - (NSComparisonResult)sortByFormat:(RadioStation *)anObject
{
if (self.format < anObject.format) {
    return NSOrderedAscending;
} else if (self.format > anObject.format) {
    return NSOrderedDescending;
}
return NSOrderedSame;

}

Upvotes: 2

Related Questions