按键排序数组

时间:2011-05-29 20:46:43

标签: objective-c xcode

我使用NSMutableArray(listId)制作dictionaries(dizionario)(通过调用sqlite数据库):

-(void)readItems {
    NSString * query = @"SELECT * FROM item";
    NSArray * arrayQuery = [[NSArray alloc] initWithObjects:@"id",@"title",@"note",@"icon",@"perc",@"complete",nil];
    NSArray * arrayEle = [self caricaValoriDaDBStandard:query :arrayQuery];

    for (int i = 0; i < [arrayEle count]; i++)
    {
        NSMutableDictionary *dizionario = [arrayEle objectAtIndex:i];
        [listId addObject:dizionario];
    }
}

如何对数组进行排序,例如“id”键?

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码:

NSArray sortedArray = [listId sortedArrayUsingComparator: ^(id a, id b) {
    NSString *first = [a objectForKey:@"id"];
    NSString *second = [b objectForKey:@"id"];
    return [first compare:second];
}];