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 ;}}}}