iOS array arrangement

Source: Internet
Author: User

1. This method can realize the flashback output of the array.

Reverseobjectenumerator

Nsarray *array = @[@ "1", @ "2", @ "3", @ "4"];

For (NSString *string in [array Reverseobjectenumerator]) {

NSLog (@ "string =====%@", string);

}

Log logs:

2016-06-10 16:28:54.458 Custom Search [4242:322108] string ===== 4

2016-06-10 16:28:54.459 Custom Search [4242:322108] string ===== 3

2016-06-10 16:28:54.460 Custom Search [4242:322108] string ===== 2

2016-06-10 16:28:54.461 Custom Search [4242:322108] string ===== 1

2, this method can also achieve flashback arrangement

[Array enumerateobjectswithoptions:nsenumerationreverse usingblock:^ (id _nonnull obj, nsuinteger idx, BOOL * _Nonnull St OP) {

NSLog (@ "obj = =%@", obj);

}];

Logo log:

2016-06-10 16:35:59.281 Custom Search [4295:326175] obj = = 4

2016-06-10 16:36:07.952 Custom Search [4295:326175] obj = = 3

2016-06-10 16:36:15.771 Custom Search [4295:326175] obj = = 2

2016-06-10 16:36:20.927 Custom Search [4295:326175] obj = = 1

3. Array sorting

Nsarray *arr = [Nsarray arraywithobjects:@ "", "@", @ "Si", @ "0", @ "EF", nil];

Nsarray *sortarr = [arr sortedarrayusingselector: @selector (compare:)];

NSLog (@ "Sortarr = =%@", Sortarr);

Logo log:

2016-06-10 17:07:36.509 Custom Search [4526:344425] Sortarr = = (

0,

23,

34,

54,

Ef

)

4. Custom sorting

First create the Person class

@interface Person:nsobject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, assign) Nsinteger age;

+ (person*) personwithage: (int) Age Withname: (NSString *) name;

-(Nscomparisonresult) Compareperson: (person *) person;

@end

Implement both methods in the. m file

+ (person *) Personwithage: (int.) Age Withname: (NSString *) name

{

Person *p = [[Person alloc] init];

P.name = name;

P.age = age;

return p;

}

#pragma Mark Custom Sort

-(Nscomparisonresult) Compareperson: (person *) person

{

Sort by age by default

Nscomparisonresult result = [[NSNumber numberWithInt:person.age] Compare:[nsnumber NumberWithInt:self.age]];

If the age is the same, sort by name

if (result = = Nsorderedsame) {

result = [Self.name compare:person.name];

}

return result;

}

Then implement the following methods in the controller

Person *P1 = [person personwithage:24 withname:@ "Zhangshiye"];

Person *P2 = [person personwithage:22 withname:@ "Lisi"];

Person *P3 = [person personwithage:25 withname:@ "Wangwu"];

Person *P4 = [person personwithage:25 withname:@ "Liwu"];

Person *P5 = [person personwithage:21 withname:@ "Liwuxue"];

Nsarray *arrays = [Nsarray arraywithobjects:p1,p2,p3,p4,p5, Nil];

Nsarray *sortedarray = [Arrays Sortedarrayusingselector: @selector (Compareperson:)];

NSLog (@ "After sorting:%@", Sortedarray);

5. Sorting using the block syntax

Use block syntax to sort

Nsarray *dataarr = [Nsarray arraywithobjects:@ "1BC", @ "4b6", @ "123", @ "789", @ "3ef",

NIL];

Nsarray *resultarr = [Dataarr sortedarrayusingcomparator:^nscomparisonresult (ID _nonnull obj1, id _Nonnull obj2) {

Nscomparisonresult result = [Obj1 compare:obj2];

return result;

}];

NSLog (@ "After sorting:%@", Resultarr);

Logo log:

2016-06-10 17:33:32.221 Custom Search [5100:363766] after sorting :(

123,

1BC,

3EF,

4b6,

789

)

iOS array arrangement

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.