NSArray sorting methods

Source: Internet
Author: User

NSArray sorting methods

/* In general, arrays commonly used in OC are sorted by sortedArrayUsingSelector: sortedArrayUsingComparator: sortedArrayUsingDescriptors: * // * 1. sortedArrayUsingSelector, you can use sortedArrayUsingSelector: method. The Code is as follows * // void sortArray1 () {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 using the compare: method that comes with the string, you can also write the compare: method by yourself to compare objects; below: first, the Person class is created, the implementation method is as follows (the header file is saved): */# import "Person. h "@ implementation Person // directly implements the static method to obtain the Person object with name and age + (Person *) personWithAge :( int) age withName :( NSString *) name {Person * person = [[Person alloc] init]; person. age = age; person. name = name; return person;} // custom sorting method-(NSComparisonResult) comparePerson :( Person *) person {// by age NSComparisonResult result = [[NSNumber numberWithInt: person. age] compare: [NSNumber numberWithInt: self. age]; // Note: Data conversion is required for the basic data type. // if the age is the same, sort the data by name. if (result = NSOrderedSame) {result = [self. name compare: person. name];} return result;} @ end // The main function code is as follows: void sortArray2 () {Person * p1 = [Person personWithAge: 23 withName: @ "zhangsan"]; person * p2 = [Person personWithAge: 21 withName: @ "lisi"]; Person * p3 = [Person personWithAge: 24 withName: @ "wangwu"]; Person * p4 = [Person personWithAge: 24 withName: @ "liwu"]; Person * p5 = [Person personWithAge: 20 withName: @ "liwu"]; NSArray * array = [NSArray arrayWithObjects: p1, p2, p3, p4, p5, nil]; NSArray * sortedArray = [array sortedArrayUsingSelector: @ selector (comparePerson :)]; NSLog (@ "after sorting: % @", sortedArray);}/* 2. Use the block syntax (sortedArrayUsingComparator :) Apple provides the block syntax for comparison. Convenience. SortedArrayUsingComparator can be used to sort arrays. The Code is as follows: */void sortArray3 () {NSArray * array = [NSArray arrayWithObjects: @ "1bc", @ "4b6 ", @ "123", @ "789", @ "3ef", nil]; NSArray * sortedArray = [array sortedArrayUsingComparator: ^ NSComparisonResult (id obj1, id obj2) {// The code here can refer to the above compare: Default sorting method, you can also write the custom method here, and sort the object NSComparisonResult result = [obj1 compare: obj2]; return result;}]; NSLog (@ "after sorting: % @", sortedArray);}/* 3, high SortedArrayUsingDescriptors: what if this is the case? The Person class has another class variable. For example, in addition to the name and age variables, the Person class also has a Car type, and the Car class has a name attribute. Sort Person objects by Car name. If the name of a Car is the same, the Person objects are sorted by age, if the age is the same, sort by Person name. In this case, we need to use the third method, using the sort descriptor. If you are interested, please refer to the API introduction. The Code is as follows: First, write a Car class to implement a Car class. the m code is as follows: */# import "Car. h "@ implementation Car + (Car *) initWithName :( NSString *) name {Car * car = [Car alloc] init]; car. name = name; return car;} @ end // then rewrite the Person class to implement the Person class. the m code is 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; return person;} // rewrite the description method here to display the final test result-(NSString *) description {return [NSString stringWithFormat: @ "age is % zi, name is % @, car is % @ ", _ age, _ name, _ car. name] ;}@ end // The main function code is as follows: void sortArray4 () {// first three vehicles are available, audi, Rolls-Royce, and BMW Car * car1 = [Car initWithName: @ "Audio"]; Car * car2 = [Car initWithName: @ "Rolls-Royce"]; car * car3 = [Car initWithName: @ "BMW"]; // five more persons, each with a Car, Car2, car1, car1, car3, car2 Person * p1 = [Person personWithAge: 23 withName: @ "zhangsan" withCar: car2]; Person * p2 = [Person personWithAge: 21 withName: @ "zhangsan" withCar: car1]; Person * p3 = [Person personWithAge: 24 withName: @ "lisi" withCar: car1]; Person * p4 = [Person personWithAge: 23 withName: @ "wangwu" withCar: car3]; Person * p5 = [Person personWithAge: 23 withName: @ "wangwu" withCar: car2]; // Add the array NS Array * array = [NSArray arrayWithObjects: p1, p2, p3, p4, p5, nil]; // build the sorting descriptor NSSortDescriptor * carNameDesc = [NSSortDescriptor sortDescriptorWithKey: @ "car. name "ascending: YES]; NSSortDescriptor * personNameDesc = [NSSortDescriptor attributes: @" name "ascending: YES]; NSSortDescriptor * personAgeDesc = [NSSortDescriptor attributes: @" age "ascending: YES]; // put the sort descriptor in the array, and the order is the order you want to sort // Here I am: first sort by age, then the car name, and finally by the person's name NSArray * descriptorArray = [NSArray arrayWithObjects: personAgeDesc, carNameDesc, personNameDesc, nil]; NSArray * sortedArray = [array sortedArrayUsingDescriptors: descriptorArray]; NSLog (@ "% @", sortedArray);}/* sort by age first. If the age is the same, sort by car. If the car is the same, sort by name. (Note: To display strings in the preceding two sorting methods, rewrite the description method )*/

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.