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:
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:
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).
Esta web utiliza cookies para que podamos ofrecerte la mejor experiencia de usuario posible. La información de las cookies se almacena en tu navegador y realiza funciones tales como reconocerte cuando vuelves a nuestra web o ayudar a nuestro equipo a comprender qué secciones de la web encuentras más interesantes y útiles.
Cookies estrictamente necesarias
Las cookies estrictamente necesarias tiene que activarse siempre para que podamos guardar tus preferencias de ajustes de cookies.
Si desactivas esta cookie no podremos guardar tus preferencias. Esto significa que cada vez que visites esta web tendrás que activar o desactivar las cookies de nuevo.
Cookies de terceros
Esta web utiliza Google Analytics para recopilar información anónima tal como el número de visitantes del sitio, o las páginas más populares.
Dejar esta cookie activa nos permite mejorar nuestra web.
¡Por favor, activa primero las cookies estrictamente necesarias para que podamos guardar tus preferencias!