Array operation traversal, sort, reverse order

Source: Internet
Author: User
Tags vars

Array Traversal:

Index traversal

Enumeration traversal:

    1. nsenumerator* enumerator = [array objectenumerator];
    2. ID e = nil;
    3. while (e = [enumerator nextobject])
    4. {
    5. NSLog (@ "E =%@", e);
    6. }

Code block Traversal:

  1. Example 1: Enumeration traversal
  2. [Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, boolbool *stop) {
  3. NSLog (@ "obj =%@", obj);
  4. }];
  5. Example 2: Enumeration traversal, which encounters an element (Obj=array[idx] that meets the criteria) exits the traversal.
  6. [Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, boolbool *stop) {
  7. if ([obj isequaltostring:@ "E3"]) {
  8. *stop = YES; //Abort traversal, break
  9. } Else {
  10. *stop = NO; //Continue traversal, continue
  11. }
  12. }];

Array sorting:

Circular sorting

code block Ordering:

Nsarray *array = @[@12,@23,@8];

Nscomparator Sortblock = ^ (id string1,id string2) {

return [string1 compare:string2];

if ([string1 floatvalue]<[string2 Floatvalue]) {

return nsorderedascending;

}else if ([string1 floatvalue]==[string2 Floatvalue]) {

return nsorderedsame;

}else{

return nsordereddescending;

}

};

Nsarray *sortarray = [array Sortedarrayusingcomparator:sortblock];

NSLog (@ "%@", Sortarray);

}

Array in reverse order:

Traverse reverse Order

Method in reverse order:

[[Array reverseobjectenumerator] allobjects];

Array operation traversal, sort, reverse order

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.