0

I make an NSMutableArray(listId) (through a call to a sqlite database) with dictionaries(dizionario):

-(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];
    }
}

How do I sort the array, for example, for the "id" key?

1
  • Cleaned up your code a bit. Try not to use tabs in your code pieces on SO. Replace them with 4 spaces. Cheers! Commented May 29, 2011 at 21:03

1 Answer 1

3

You can use the following code:

NSArray sortedArray = [listId sortedArrayUsingComparator: ^(id a, id b) {
    NSString *first = [a objectForKey:@"id"];
    NSString *second = [b objectForKey:@"id"];
    return [first compare:second];
}];
Sign up to request clarification or add additional context in comments.

3 Comments

very nice! thanks! how do I choose to let it increasing or decreasing order?
@Vins Hope it will work! If so, do not forget to accept the answer ;)
yep,still waiting for 2 minutes, there is a limit of time to do it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.