curso programacion iphone

Sorting objects in Cocoa: Sorting using selector (in ascending order)

This is the most common sorting method for sorting objects in Cocoa. It uses a selector that must return an NSComparisonResult (either NSOrderedAscending, NSOrderedSame, or NSOrderedDescending).
The most common selector is compare:.  MUST be declared in header file, as NSObject doesn’t implement it. All custom classes should implement compare:
- (void)sortUsingSelector:(SEL)comparator 
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator
If your NSMutableArray is full of NSStrings or NSNumbers, you can just do:
[myArray sortUsingSelector:@selector(compare:)];
and it will sort them appropriately because NSString and NSNumber both properly implement compare:. The idea is that every class should implement a compare: method that makes the receiver compare itself and return the right result.
sortUsingSelector: gets more interesting when you have objects that aren’t simply strings or numbers. For instance, let’s say I have a class Person with instance variables (and corresponding accessor methods) firstName and lastName whose objects I want to be able to sort by name (in the form “Last, First”). I can implement something like this:
- (NSComparisonResult)comparePerson:(Person *)p
{
    return [[NSString stringWithFormat:@"%@, %@",
            [self lastName], [self firstName]]
            compare:
            [NSString stringWithFormat:@"%@, %@",
                                       [p lastName],
                                       [p firstName]];
}
Using this method with sortUsingSelector: will sort all Nelsons before all Smiths, and Abby Smith before Bernard Smith.
Of course, you can make things more flexible by deferring the sort order until runtime. Sometimes you may want to sort by first name instead of last name. In this case, the best thing to do is probably something like this:
- (NSComparisonResult)comparePerson:(Person *)p
{
    return [[self stringForSorting] compare:[p stringForSorting]];
}

- (NSString *)stringForSorting
{
    if (something)  // determine sorting type here
        return [NSString stringWithFormat:@"%@, %@",
                                          [self lastName],                                               [self firstName]];
    // else…
        return [NSString stringWithFormat:@"%@ %@",
                                          [self firstName],                                             [self lastName]];
}
You would replace the “if (something)” condition with something useful; maybe check a user preference, or the state of something in the GUI.

Sorting using Descriptors

This is a more complex and flexible way of sorting. Can sort in ascending and descending order and using several attributes(ie, sort first by attribute name in ascending order, then by attribute  age in descending order).
The most common methods are:
- (void)sortUsingDescriptors:(NSArray *)sortDescriptors
- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors
Both take an array of NSSortDescriptorObjects
NSSortDescriptor
+ sortDescriptorWithKey:ascending:
- initWithKey:ascending:
+ sortDescriptorWithKey:ascending:selector:
- initWithKey:ascending:selector:
+ sortDescriptorWithKey:ascending:comparator:
- initWithKey:ascending:comparator:
Example:

Sorting using Blocks

You provide an NSComparator block that takes the 2 objects and returns an NSComparisonResult.
The relevant methods are:
- (void)sortUsingComparator:(NSComparator)cmptr
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
Example (from 10 uses of blocks):
float target = 5.0f;
[someArray sortedArrayUsingComparator:^(id obj1, id obj2) {
    float diff1 = fabs([obj1 floatValue] – targetValue);
    float diff2 = fabs([obj2 floatValue] – targetValue)
    if ( diff1 < diff2 )
        return NSOrderedAscending;
    else if ( diff1 > diff2 )
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}];

Which one should you use?

For default sorting, implement compare: and use sortUsingSelector: @selector(compare:).
For quickies, use blocks or NSSortDescriptors
Tagged with:  

2 Responses to Sorting objects in Cocoa

  1. memmaker650 says:

    Creo que le ha pasado algo a algún plugin del blog porque el artículo es muy dificil de leer.

    Supongo que ahora que has añadido plugin nuevo para el coloreado sintáctico de Objetive-C puedes mejorarlo.

  2. OnEe says:

    Genace9ia da Silva Alberton diz:Prezados Amigos , Tendo em vista a visibilidade do IMAP, ne3o he1 psiisbilodade de curso e0 diste2ncia? Esse sistema possibilitaria a mescla de informae7f5es de estudiosos e interessados pelo tema mediae7e3o. Fica a sugeste3o . A Escola Superior da Magistratura do Rio Grande do Sul Brasil desenvolve cursos e0 diste2ncia em outras e1reas, mas seria interessante uma parceria em mediae7e3o . Cordialmente, Genace9ia da Silva Alberton- Nfacleo de Estudos de Mediae7e3o RS- Brasil.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>