Use block variables to sort character array objects and block them

Source: Internet
Author: User

Use block variables to sort character array objects and block them

<Span style = "font-size: 18px;"> sort in descending order </span>
<span style="font-size:18px;">NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"abc",@"bcd",@"123",@"9987", nil];                NSComparisonResult (^block)(NSString *str1, NSString *str2) = ^(NSString *str1, NSString *str2){            return -[str1 compare:str2];        };        NSArray *arr1 =[arr sortedArrayUsingComparator:block];        NSLog(@"%@",arr1);</span>
<Span style = "font-size: 18px;"> sort in ascending order </span>

NSMutableArray * arr = [NSMutableArray arrayWithObjects: @ "abc", @ "bcd", @ "123", @ "9987", nil]; NSComparisonResult (^ block) (NSString * str1, NSString * str2) = ^ (NSString * str1, NSString * str2) {return [str1 compare: str2];};

NSArray * arr1 = [arr sortedArrayUsingComparator: block];

NSLog (@ "% @", arr1 );



Sort arrays by pointer Variables

# Include <stdio. h>
# Include <string. h>
Void main ()
{
Int I, k = 0, c;
Char a [1, 100], B [2, 200];
Scanf ("% s", );
C = strlen ();
Char * p =;
For (I = 1; I <c; I + = 2)
B [k ++] = * (p + I );
For (I = 0; I <c; I + = 2)
B [k ++] = * (p + I );
P = B;
For (I = 0; I <c; I ++)
Printf ("% c", * (p + I ));
Printf ("\ n ");
}

In the java language, the object array can sort the array objects based on certain properties of objects?

// Add a static method to name and score // manually write the sorting method. If there is a code error, please forgive me. The idea is correct. public void sortTest (Student [] test) {Student s = new Student (); for (int I = 0; I <test. length; I ++) {for (int j = test. length-1; j> I; j --) {if (test [j]. score <test [J-1]. score) {s = test [j]; test [j] = test [J-1]; test [J-1] = s ;}}}}

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.