Four sorting methods of arrays nsarray in objective-C Foundation

Source: Internet
Author: User

The Demo code is as follows:
There are four methods to sort Arrays:
1. Basic sorting method:
Use the compare method provided in nsstring for sorting. For details, see arraysort1 of the demo.
2. Custom compare
Customize the comparestudent method in the object class to implement the sorting Logic
3. sort by block:
Sortedarrayusingcomparator
4. advanced sorting method
Use the sorting descriptor to customize the sorting priority

Book. h file:
# Import <Foundation/Foundation. h>
@ Interface book: nsobject

@ Property (nonatomic, retain) nsstring * Name;
+ (ID) bookwithname :( nsstring *) Name;
@ End

Book. M file:
# Import "book. H"
# Import "book. H"

@ Implementation book
+ (ID) bookwithname :( nsstring *) name {
Book * book = [[[Book alloc] init] autorelease];
Book. Name = Name;
Return book;
}

-(Void) dealloc {
[_ Name release];
[Super dealloc];
}
@ End

Student. h file:
# Import <Foundation/Foundation. h>

@ Class book;
@ Interface Student: nsobject

@ Property (nonatomic, retain) nsstring * firstname;
@ Property (nonatomic, retain) nsstring * lastname;
@ Property (nonatomic, retain) Book * book;

+ (ID) studentwithfirstname :( nsstring *) firstname lastname :( nsstring *) lastname;
+ (ID) studentwithfirstname :( nsstring *) firstname lastname :( nsstring *) lastname bookname :( nsstring *) bookname;

-(Nscomparisonresult) comparestudent :( student *) Stu;
@ End

Student. M file:

# Import "student. H"
# Import "book. H"
@ Implementation student

+ (ID) studentwithfirstname :( nsstring *) firstname lastname :( nsstring *) lastname {
Student * Stu = [[STUDENT alloc] init] autorelease];
Stu. firstname = firstname;
Stu. lastname = lastname;
Return Stu;
}

+ (ID) studentwithfirstname :( nsstring *) firstname lastname :( nsstring *) lastname bookname :( nsstring *) bookname {
Student * Stu = [STUDENT studentwithfirstname: firstname lastname: lastname];
Stu. Book = [Book bookwithname: bookname];

Return Stu;
}

// The return value type must be nscomparisonresult
-(Nscomparisonresult) comparestudent :( student *) Stu {
// Sort the last name
Nscomparisonresult result = [self. lastname compare: Stu. lastname];
// If the last name is the same, sort by name
If (result = nsorderedsame ){
Result = [self. firstname compare: Stu. firstname];
}
Return result;
}
// Rewrite the description method
-(Nsstring *) Description {
Return [nsstring stringwithformat: @ "% @
% @-% @ ", Self. lastname, self. firstname, self. Book. Name];
}

-(Void) dealloc {
[_ Firstname release];
[_ Lastname release];
[_ Book release];
[Super dealloc];
}

@ End

Main test file:

# Import <Foundation/Foundation. h>
# Import "student. H"

# Pragma mark sort1
Void arraysort1 (){
Nsarray * array = [nsarray arraywithobjects: @ "1", @ "5", @ "2", @ "8", @ "3", nil];
// Comparison method of the specified Element compare
Nsarray * array2 = [array sortedarrayusingselector: @ selector (compare :)];
Nslog (@ "array2: % @", array2 );
}

Void arraysort2 (){

Student * stu1 = [STUDENT studentwithfirstname: @ "1" lastname: @ "Wang"];
Student * stu2 = [STUDENT studentwithfirstname: @ "1" lastname: @ "Li"];
Student * stu3 = [STUDENT studentwithfirstname: @ "2" lastname: @ "Wang"];
Student * stu4 = [STUDENT studentwithfirstname: @ "3" lastname: @ "Zhang"];

Nsarray * array = [nsarray arraywithobjects: stu1, stu2, stu3, stu4, nil];
Nsarray * array2 = [array sortedarrayusingselector: @ selector (comparestudent :)];
Nslog (@ "% @", array2 );
}

Void arraysort3 (){
Student * stu1 = [STUDENT studentwithfirstname: @ "1" lastname: @ "Wang"];
Student * stu2 = [STUDENT studentwithfirstname: @ "1" lastname: @ "Li"];
Student * stu3 = [STUDENT studentwithfirstname: @ "2" lastname: @ "Wang"];
Student * stu4 = [STUDENT studentwithfirstname: @ "3" lastname: @ "Zhang"];

Nsarray * array = [nsarray arraywithobjects: stu1, stu2, stu3, stu4, nil];

Nsarray * array2 = [array sortedarrayusingcomparator: ^ nscomparisonresult (student * obj1, student * obj2 ){
// Sort the last name
Nscomparisonresult result = [obj1.lastname compare: obj2.lastname];
// If the last name is the same, sort by name
If (result = nsorderedsame ){
Result = [obj1.firstname compare: obj2.firstname];
}
Return result;
}];

Nslog (@ "array2: % @", array2 );
}
# Pragma mark custom priority sorting
Void arraysort4 (){
Student * stu1 = [STUDENT studentwithfirstname: @ "1" lastname: @ "Wang" bookname: @ "book1"];
Student * stu2 = [STUDENT studentwithfirstname: @ "1" lastname: @ "Li" bookname: @ "book3"];
Student * stu3 = [STUDENT studentwithfirstname: @ "2" lastname: @ "Wang" bookname: @ "book1"];
Student * stu4 = [STUDENT studentwithfirstname: @ "3" lastname: @ "Zhang" bookname: @ "book2"];

Nsarray * array = [nsarray arraywithobjects: stu1, stu2, stu3, stu4, nil];
// Create a sorting descriptor @ "lastname" @ property with the same name
Nssortdescriptor * booknamedesc = [nssortdescriptor sortdescriptorwithkey: @ "book. Name" ascending: Yes];
Nssortdescriptor * lastnamedesc = [nssortdescriptor sortdescriptorwithkey: @ "lastname" ascending: Yes];
Nssortdescriptor * firstnamedesc = [nssortdescriptor sortdescriptorwithkey: @ "firstname" ascending: Yes];
// Sort the priorities according to requirements
Nsarray * DESC = [nsarray arraywithobjects: booknamedesc, lastnamedesc, firstnamedesc, nil];
Nsarray * array2 = [array sortedarrayusingdescriptors: DESC];

Nslog (@ "array2: % @", array2 );
}

Int main (INT argc, const char * argv [])
{

@ Autoreleasepool {

Arraysort1 ();
Nslog (@ "------ arraysort2 ------");
Arraysort2 ();
Nslog (@ "------ arraysort3 -------");
Arraysort3 ();
Nslog (@ "------ arraysort4 -------");
Arraysort4 ();
}
Return 0;
}

Print the result:
02:27:44. 449 nsarray sorting [3531: 303] array2 :(
1,
2,
3,
5,
8
)
02:27:44. 473 nsarray sorting [3531: 303] ------ arraysort2 ------
02:27:44. 475 nsarray sorting [3531: 303] (
"Li 1-(null )",
"Wang 1-(null )",
"Wang 2-(null )",
"Zhang 3-(null )"
)
02:27:44. 476 nsarray sorting [3531: 303] ------ arraysort3 -------
02:27:44. 478 nsarray sorting [3531: 303] array2 :(
"Li 1-(null )",
"Wang 1-(null )",
"Wang 2-(null )",
"Zhang 3-(null )"
)
02:27:44. 478 nsarray sorting [3531: 303] ------ arraysort4 -------
02:27:44. 479 nsarray sorting [3531: 303] array2 :(
"Wang 1-book1 ",
"Wang 2-book1 ",
"Zhang 3-book2 ",
"Li 1-book3"
)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.