Several simple and effective array sorting methods for IOS, and ios array sorting

Source: Internet
Author: User

Several simple and effective array sorting methods for IOS, and ios array sorting

// First, use the sortedArrayUsingComparator of the array to call the object in the NSComparator, obj1 and obj2 arrays.

NSComparator cmptr = ^ (id obj1, id obj2 ){
If ([obj1 integerValue]> [obj2 integerValue]) {
Return (NSComparisonResult) NSOrderedDescending;
}
 
If ([obj1 integerValue] <[obj2 integerValue]) {
Return (NSComparisonResult) NSOrderedAscending;
}
Return (NSComparisonResult) NSOrderedSame;
};

NSArray * sortArray = [[NSArray alloc] initWithObjects: @ "1", @ "3", @ "4", @ "7", @ "8 ", @ "2", @ "6", @ "5", @ "13", @ "15", @ "12", @ "20", @ "28 ", @ "", nil];
// Before sorting
NSMutableString * outputBefore = [[NSMutableString alloc] init];
For (NSString * str in sortArray ){
[OutputBefore appendFormat: @ "];
}
NSLog (@ "Before sorting: % @", outputBefore );
[OutputBefore release];
// First sort
NSArray * array = [sortArray sortedArrayUsingComparator: cmptr];
 
NSMutableString * outputAfter = [[NSMutableString alloc] init];
For (NSString * str in array ){
[OutputAfter appendFormat: @ "];
}
NSLog (@ "after sorting: % @", outputAfter );
[OutputAfter release];


The second sorting method uses sortedArrayUsingFunction to call the corresponding customSort method. In this method, obj1 and obj2 are the objects in the index group respectively.
NSInteger customSort (id obj1, id obj2, void * context ){
If ([obj1 integerValue]> [obj2 integerValue]) {
Return (NSComparisonResult) NSOrderedDescending;
}
 
If ([obj1 integerValue] <[obj2 integerValue]) {
Return (NSComparisonResult) NSOrderedAscending;
}
Return (NSComparisonResult) NSOrderedSame;
}

NSArray * sortArray = [[NSArray alloc] initWithObjects: @ "1", @ "3", @ "4", @ "7", @ "8 ", @ "2", @ "6", @ "5", @ "13", @ "15", @ "12", @ "20", @ "28 ", @ "", nil];
// Before sorting
NSMutableString * outputBefore = [[NSMutableString alloc] init];
For (NSString * str in sortArray ){
[OutputBefore appendFormat: @ "];
}
NSLog (@ "Before sorting: % @", outputBefore );
[OutputBefore release];
 
NSArray * array = [sortArray sortedArrayUsingFunction: customSort context: nil];
 
NSMutableString * outputAfter = [[NSMutableString alloc] init];
For (NSString * str in array ){
[OutputAfter appendFormat: @ "];
}
NSLog (@ "after sorting: % @", outputAfter );
[OutputAfter release];


Third, use sortUsingDescriptors to call NSSortDescriptor

NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @ "price" ascending: NO]; // where price is the attribute of the object in the array, it is more concise and convenient to store objects in arrays.
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects: & sortDescriptor count: 1];
[_ TotalInfoArray sortUsingDescriptors: sortDescriptors];
[_ AirListView refreshTable: _ totalInfoArray];
[SortDescriptor release];
[SortDescriptors release];

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.