1 Description of functions and examples
Function One:-(void) Sortusingselector: (SEL) comparator;
Applies to elements in the array when the comparison function is used;
The array sort function, which calls the function's object to an array, comparator is the method that invokes the elements in the function's arrays. The function parameter type is an element type or ID type in an array, does not need to pass parameters when called, and the sorting process is not visible when the function executes: loop through each element, compare it, and put it in the appropriate position
Usage examples:
Sorts the elements in the array by the size of the string:
Nsmutablearray*array = [[Nsmutablearray alloc] initwithobjects:@ "white" @ "Blue" @ "Red" @ "Black", nil];
[Arraysortusingselector: @selector (compare:)];
NSLog (@ "sorted array:%@", array);
The run result is: sorted array:
(
Black,
Blue,
Red,
White
)
Explanation: When calling the Sortusingselector () method, we specify to use the Compare: method to compare. It is possible that the type is used internally to judge, because the type of comparison is nsstring, so the compare of NSString is called: method. The process of sorting is not visible, but the process is: take out each element, use compare: Compare, and then put it in the right place. For compare: The function is already defined in the extension (category) of the NSString class. The system already knows how to determine the A string and B string who is larger, for their own well-defined class, you need to define the Compare method, and specify a sort parameter (such as we define a sudent type, and then set the sorting by the ID of the row.
function two-(void) Sortusingfunction: (Nsinteger (*) (ID, id, void *)) compare Ontext: (void*) context;
It is recommended to use an element in an array without a comparison function.
How to use: simply define the comparison function in the class that calls the comparison function as follows:
Nsinteger Sortobjectsbypatientid (ID obj1, id obj2,void *context)
{
NSSTRING*D1 = [(study*) obj1 patientid];//obj1 obj2 An element in an array, Patientid is one of its properties
NSSTRING*D2 = [(study*) obj2 Patientid];
Return[d2 COMPARE:D1];
}
Then call the comparison function:
[Listdataarraysortusingfunction:sortobjectsbypatientname Context:null];
Function three:-(void) Sortusingselector: (SEL) comparator; function.
With this function, the elements in the array need to have a comparison method.
First define the element class, in which you implement the comparison method:
Class definition
@interface Study:nsobject
{
Nsstring*patientid;
}
-(Nscomparisonresult) Compareid: (study*) dic;
@end;
Class implementation
@implementation Study
-(Nscomparisonresult) Compareid: (study*) dic
{
Return[patientid Compare:dic.patientID];
}
@end;
To call the sort method in another class:
@interface sorttest:nsobject<nscopying,nscoding>
{
Nsmutablearray *listarrray; A mutable array used to store Addresscard objects
}
-(void) sort
{
[Listarrray sortusingselector: @selector (Compareid:)];//sort method, Listarrray stores all study type objects The way to compare is to call Compareid: The method
}
How to sort arrays in iOS