Block array sorting

Source: Internet
Author: User

//

Main.m

Block jobs

//

Created by lanou3g on 14-11-20.

Copyright (c) 2014 Lanou_zhou. All rights reserved.

//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

{

Definition? A block with a return value of bool, with two nsstring parameters. Implementation: Sentencing

The broken strings are equal.

BOOL (^comparestring) (NSString *, nsstring *) = ^ (NSString *str1, NSString *str2) {

if ([str1 isequaltostring:str2]) {

return YES;

}else{

return NO;

}

};

if (CompareString (@ "SSS", @ "SSS")) {

NSLog (@ "Same");

}

Definition? A block, the return value is Nsinteger, there are two parameters,? One is Nsarray, one is nsstring. Implementation: The array is judged to contain this string, if included, returns the subscript of the string, if not included, returns-1.

Nsarray *arr = @[@ "AA", @ "ss", @ "WW"];

NSString *str = @ "ss";

Int (^find) (Nsarray *, nsstring *) = ^ (Nsarray *arr, NSString *str) {

for (int i = 0; i < Arr.count; i++) {

if ([Arr[i] isequaltostring:str]) {

return i;

}

}

return-1;

};

int f = Find (ARR,STR);

NSLog (@ "%d", f);

Create an array that is initialized to @ "123", @ "21", @ "33", @ "69", @ "108",

@ "256". To use the block syntax, to sort the array of rows in. and output the content. Sorted results: 108 123 21 256 33 69

Reference: Sortedarraywithoptions:usingcomparator:

Non-variable groups

Nsarray *arr1 = @[@ "23", @ "1", @ "3", @ "2", @ "108"];

Nsarray *ARR2 = [arr1 sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {

if ([Obj1 intvalue] > [obj2 intvalue]) {

return nsordereddescending;

}

return nsorderedsame;

}

];

NSLog (@ "sortarray1:%@", arr2);

Nsarray *arr11 = @[@ "23", @ "1", @ "3", @ "2", @ "108"];

Nsarray *a1 = [Arr11 sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {

if ([Obj1 intvalue] > [obj2 intvalue]) {

return nsordereddescending;

}

return nsorderedsame;

}

];

NSLog (@ "sortarray1:%@", A1);

5, block for variable array sorting

5,1

Nscomparator Sortblock = ^ (ID string1, id string2)

{

return [string1 compare:string2];

};

Nsmutablearray *stringarray = [Nsmutablearray arraywithobjects:@ "G", @ "C", @ "B", @ "a", nil];

Nsarray *a = [Stringarray sortedarrayusingcomparator:sortblock];

NSLog (@ "array:%@", a);

Block immutable groups written separately

Nscomparator SortBlock1 = ^ (ID string1, id string2)

{

return [string1 compare:string2];

};

Nsarray *stringarray2 = [Nsarray arraywithobjects:@ "G", @ "C", @ "B", @ "a", nil];

Nsarray *a2 = [StringArray2 sortedarrayusingcomparator:sortblock1];

NSLog (@ "block variable array written separately:%@", A2);

return 0;

}

Block array 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.