#pragma mark Array Sort 1
void ArraySort1 () {
Nsarray *array = [Nsarray arraywithobjects:@ "2", @ "3", @ "1", @ "4", nil Nil];
Returns an ordered array that does not change the order of the elements in the original array
The comparison method for the specified element: compare:
Nsarray *array2 = [Array sortedarrayusingselector: @selector (compare:)];
NSLog (@ "array2:%@", array2);
}
#pragma mark Array Sort 2
void ArraySort2 () {
Student *STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li"];
Student *STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang"];
Student *STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li"];
Student *stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao"];
Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil];
Specify a comparison method for sorting
Nsarray *array2 = [Array sortedarrayusingselector: @selector (comparestudent:)];
NSLog (@ "array2:%@", array2);
}
-(Nscomparisonresult) Comparestudent: (Student *) Stu {
Sort by last name first
Nscomparisonresult result = [Self.lastname compare:stu.lastname];
If you have the same last name, compare the names.
if (result = = Nsorderedsame) {
result = [Self.firstname compare:stu.firstname];
}
return result;
}
#pragma mark Array Sort 3
void ArraySort3 () {
Student *STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li"];
Student *STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang"];
Student *STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li"];
Student *stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao"];
Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil];
Sort by block
Nsarray *array2 = [Array sortedarrayusingcomparator:
^nscomparisonresult (Student *obj1, Student *obj2) {
Sort by last name first
Nscomparisonresult result = [Obj1.lastname compare:obj2.lastname];
If you have the same last name, compare the names.
if (result = = Nsorderedsame) {
result = [Obj1.firstname compare:obj2.firstname];
}
return result;
}];
NSLog (@ "array2:%@", array2);
}
#pragma mark Array Sort 4-advanced sort
void ArraySort4 () {
Student *STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li" bookname:@ "Book1"];
Student *STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang" bookname:@ "Book2"];
Student *STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li" bookname:@ "Book2"];
Student *stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao" bookname:@ "Book1"];
Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil];
1. Sort by Title first
The key here is the name of @property.
Nssortdescriptor *booknamedesc = [Nssortdescriptor sortdescriptorwithkey:@ "Book.name" Ascending:YES];
2. Sort by last Name
Nssortdescriptor *lastnamedesc = [Nssortdescriptor sortdescriptorwithkey:@ "LastName" Ascending:yes];
3. Sort by name again
Nssortdescriptor *firstnamedesc = [nssortdescriptor sortdescriptorwithkey:@ "FirstName" Ascending:yes];
Add a sort descriptor sequentially
Nsarray *descs = [Nsarray arraywithobjects:booknamedesc, Lastnamedesc, Firstnamedesc, nil Nil];
Nsarray *array2 = [array Sortedarrayusingdescriptors:descs];
NSLog (@ "array2:%@", array2);
}