Q:

How to sort NSMutableArray by object's pararameter

I've got NSMutableArray with objects Link which has parameter Byte taskOne. I have to sort objects by this parameter.
    for (Byte i = 0; i < [linksArray count]-1; i++) {
        Link *l = [linksArray objectAtIndex:i];
        Link *lNext = [linksArray objectAtIndex:i+1];
        if (l.taskOne >= lNext.taskOne) {
            NSLog(@"%d >= %d", l.taskOne, lNext.taskOne);
            [linksArray replaceObjectAtIndex:i withObject:lNext];
            [linksArray replaceObjectAtIndex:i+1 withObject:l];
        }
    }

this give me wrong results, but i don't know what's wrong. 
thank you for help.

A:

The easiest way to sort an array is to use NSSortDescriptor with KVC, like this:
NSArray *linksArray;    // Assume that this contains objects which respond to the -taskOne method

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"taskOne" ascending:YES];
NSArray *sortedLinks = [linksArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

This will create a new array. To sort an NSMutableArray in place, do this:
NSMutableArray *linksArray; // Assume that this contains objects which respond to the -taskOne method

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"taskOne" ascending:YES];
[linksArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

