Some methods of Nsarray sorting

Source: Internet
Author: User

/*In general, there are several ways to sort the array commonly used in OC: Sortedarrayusingselector:sortedarrayusingcomparator: Sortedarrayusingdescriptors:*//*1, simple sort (sortedarrayusingselector:) If you just sort the string, you can take advantage of the Sortedarrayusingselector: method, the code is as follows*///Simple SortingvoidsortArray1 () {Nsarray*array = [Nsarray arraywithobjects:@"ABC",@"456",@"123",@"789",@"EF", nil]; Nsarray*sortedarray =[Array sortedarrayusingselector: @selector (compare:)]; NSLog (@"after sorting:%@", Sortedarray);}/*of course, in addition to the use of string compare: Method, you can also write their own compare: methods, the comparison of objects, as follows: The first is the new person class, the implementation method is as follows (the header file is saved):*/#import "Person.h"@implementation Person//implement static methods directly to get a person object with name and age+ (person *) Personwithage: (int) Age Withname: (NSString *) name{ person*person =[[Person alloc] init]; Person.age=Age ; Person.name=name; returnPerson ;}//Custom Sorting methods-(Nscomparisonresult) Compareperson: (Person *) person{//sort by age by defaultNscomparisonresult result = [[NSNumber numberWithInt:person.age] Compare:[nsnumber NumberWithInt:self.age]];//Note: Basic data types are being converted to data//If you are of the same age, sort by name    if(Result = =nsorderedsame) {Result=[Self.name Compare:person.name]; }    returnresult;}@end//The main function code is as follows:voidSortArray2 () { person*P1 = [Person personwithage: atWithname:@"Zhangsan"]; person*P2 = [Person personwithage: +Withname:@"Lisi"]; person*P3 = [Person personwithage: -Withname:@"Wangwu"]; person*P4 = [Person personwithage: -Withname:@"Liwu"]; person*P5 = [Person personwithage: -Withname:@"Liwu"]; Nsarray*array =[Nsarray arraywithobjects:p1,p2,p3,p4,p5, Nil]; Nsarray*sortedarray =[Array sortedarrayusingselector: @selector (Compareperson:)]; NSLog (@"after sorting:%@", Sortedarray);}/*2, using block Syntax (sortedarrayusingcomparator:) Apple has provided the block syntax, more convenient. Where array ordering can be used Sortedarrayusingcomparator: method, the code is as follows:*/voidsortArray3 () {Nsarray*array = [Nsarray arraywithobjects:@"1BC",@"4b6",@"123",@"789",@"3ef", nil]; Nsarray*sortedarray = [Array Sortedarrayusingcomparator:^nscomparisonresult (IDObj1,IDobj2) {   //Here the code can refer to the above compare: The default sorting method, you can also write a custom method here, to sort objectsNscomparisonresult result =[Obj1 Compare:obj2]; returnresult;    }]; NSLog (@"after sorting:%@", Sortedarray);}/*3, Advanced sort (sortedarrayusingdescriptors:) If this is the case? There is another class variable in the person class, such as the person class in addition to the name,age variable, there is a car type, the car class has a name attribute. To sort the person objects, there are requirements: Sort by car name, if the same car, that is, car name is the same, then sorted by age, if the same age, and finally sorted by person's name. The above is going to use the third method, using the sort descriptor, not much to say, interested to see the API introduction. The code is as follows: First write a car class, implement the class CAR.M code as follows:*/#import "Car.h"@implementationCar+ (Car *) Initwithname: (NSString *) name{Car*car =[Car alloc] init]; Car.name=name; returncar;}@end//then rewrite the person class, implementing the class PERSON.M code as follows:#import "Person.h"#import "Car.h"@implementation Person+ (person *) Personwithage: (int) Age Withname: (NSString *) name Withcar: (Car *) car{ person*person =[[Person alloc] init]; Person.age=Age ; Person.name=name; Person.car=car; returnPerson ;}//This overrides the description method for the final test of the sort result display-(NSString *) description{return[NSString stringWithFormat:@"Age was%zi, name is%@, car is%@", _age,_name,_car.name];}@end//The main function code is as follows:voidSortArray4 () {//first of all, 3 cars, namely Audi, Rolls-Royce, BMWCar *car1 = [car initwithname:@"Audio"]; Car*CAR2 = [Car initwithname:@"Rolls-Royce"]; Car*CAR3 = [Car initwithname:@"BMW"]; //another 5 person, each to send a car, respectively, CAR2, Car1, Car1, Car3, Car2Person *P1 = [Person personwithage: atWithname:@"Zhangsan"WITHCAR:CAR2]; person*P2 = [Person personwithage: +Withname:@"Zhangsan"WITHCAR:CAR1]; person*P3 = [Person personwithage: -Withname:@"Lisi"WITHCAR:CAR1]; person*P4 = [Person personwithage: atWithname:@"Wangwu"WITHCAR:CAR3]; person*P5 = [Person personwithage: atWithname:@"Wangwu"WITHCAR:CAR2]; //adding arraysNsarray *array =[Nsarray arraywithobjects:p1,p2,p3,p4,p5, Nil]; //Build a Sort descriptorNssortdescriptor *carnamedesc = [Nssortdescriptor sortdescriptorwithkey:@"Car.name"Ascending:yes]; Nssortdescriptor*personnamedesc = [Nssortdescriptor sortdescriptorwithkey:@"name"Ascending:yes]; Nssortdescriptor*personagedesc = [Nssortdescriptor sortdescriptorwithkey:@" Age"Ascending:yes]; //Put the sort descriptor in the array and put it in the order that you want it to be sorted.//I am here: first sort by age, then the name of the car, and finally, by the name of the personNsarray *descriptorarray =[Nsarray Arraywithobjects:personagedesc,carnamedesc,personnamedesc, Nil]; Nsarray*sortedarray =[Array Sortedarrayusingdescriptors:descriptorarray]; NSLog (@"%@", Sortedarray);}/*As seen from the results, sort by age first, if age is the same, sort by car, if car is the same, sort by name. (Note: The above two sorting methods to implement the string display, please override the description method)*/

The article turned from a dark horse before the seniors, the author said that educated, the article status as follows:

http://850361034.blog.163.com/blog/static/32803809201436111445914/

Some methods of Nsarray sorting

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.