Sort of Nsarray

Source: Internet
Author: User
Tags array sort

Nsarray derivation of a new collection

-(Nsarray *) Arraybyaddingobject: (ID) anobject

Add a new Array,and return a new Nsarray

-(Nsarray *) Arraybyaddingobjectsfromarray: (Nsarray *) Anotherarray

Add all the elements Inanother array and return a new Nsarray original array not change

-(Nsarray *) Subarraywithrange: (nsarray) Range

Load elements within range

#pragma derive a new array

void Arraynew () {

Nsarray *array=[nsarray arraywithobjects:@ "1", @ "2", nil];

Nsarray *array2=[arrayarraybyaddingobject:@ "3"];//merged array

The original array did not change, just the return array2 changed.

Nsarray *array3=[arrayarraybyaddingobjectsfromarray:[nsarray arraywithobject:@ "4", @ "5", NIL]];//4 elements

Nsarray *array4=[array arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil];

Nsrange range= nsmakerange (n);

Nsarray *array5=[array4 Subarraywithrange:range];

}

Other method.

-(NSString) componentsjoinedbystring: (NSString *) seperator

Use a seperator to joint all the elements as a string

Use a delimiter to stitch all the elements in an array into a single string

-(BOOL) write to File: (NSString *) path atomically: (bool) Use Auxiliaryfile

Persist a nsarray to a file

#pregma Mark Other method

void Arrayother () {

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

NSString *string=[array componentsjoinedbystring:@ ","];//1,2,3,4

NSString *[email protected] "/users/apple/desktop/array.xml";

[Arry writetofile:path atomically:yes];//create a XML file

Reading array contents from a file (file has strict formatting requirements)

NSString *[email protected] "/users/apple/desktop/array.txt";

Nsarray *array2=[nsarray Arraywithcontentsoffile:path];

}

Nsarray Sort 1

-(Nsarray *) Sortedarrayusingselector: (SEL) Comparator

Return a new sorted array, original array won't change

#pragma Mark ArraySort

void ArraySort1 () {

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

Nsarray *array2=[array sortedarrayusingselector: @selector (compare:)];

[@ "1" compare:@ "2"]; Sort by the incoming comparison method

Default order is from smaller to bigger

}

Nsarray Sort 2

For example, this is called (assuming the array contains a student object);

-(Nsarray *) Sortedarrayusingcomparator: (Comparator) cmptr

typedef nscomoarisionresult (^nscomparator) (id obj1,id obj2);

[Array sortedarrayusingcomparatorresult:^ (Student *s1,student *s2) {

return [S1.name Compare:s2.name;]

}];

Sort by name

Student.h

@interface Student:nsobject

@property (Nonatomic,retain) NSString *firstname;

@property (Nonatomic,retain) NSString *lastname;

+ (ID) studentwithfirstname: (NSString *) FirstName LastName: (*nsstring *) LastName;

Pass to @selector and return value must is Nscomparisionresult

-(Nscomparisionresult) Comparestudent: (Student *) Stu;

@end

Student.m

@implementation

+ (ID) studentwithfiestname: (NSString *) FirstName LastName: (*nsstring *) lastname{

Student *stu=[[[student Alloc]init]autorelease];

Because of static method + so we need to add autorelease.

Stu.lastname=lastname;

Stu.firstname=firstname;

return Stu;

}

We can store LastName Reult first

-(Nscomparisionresult) Comparestudent: (Student *) stu{

First sort with LastName then FirstName

Nscomparisionresult Result=[self.lastname Compare:stu.lastname];

If have same LastName then compare the FirstName

if (result==nsordersame) {

Result=[self.firstname Compare:stu.firstname];

}

return result;

}

}

-(void) dealloc{

[_FirstName release];

[_lastname release];

[Super Dealloc];

}

Rewrite Description Method

Yesi

-(NSString *) description{

return [NSString stringwithformat:@ "[%@%@]", self.lastname,self.firstname];

}

@end

-----------------------

#progma Mark Array Sort 2

#import "Student.h"

void ArraySort2 () {

Student *stu1=[student studentwithfirstname::@ "Yesi" lastname:@ "Huang"];

Student *stu2=[student studentwithfirstname::@ "Jujubee" lastname:@ "Sharma"];

Student *stu3=[student studentwithfirstname::@ "Hitu" lastname:@ "Sharma"];

Student *stu4=[student studentwithfirstname::@ "Madani" lastname:@ "Green"];

Nsarray *array=[nsarray Arraywithobjects:stu1,stu2,stu3,stu4,nil];

Now need to sort with FirstName then LastName

Nsarray *array2=[array sortedarrayusingselector: @selctor (comparestudent)];

}

Nsarray Sort 3 Use block (block completely not understand OK, roleback to see again)

#pragma mark Using block to sort

#import "Student.h"

void ArraySort3 () {

Student *stu1=[student studentwithfirstname::@ "Yesi" lastname:@ "Huang"];

Student *stu2=[student studentwithfirstname::@ "Jujubee" lastname:@ "Sharma"];

Student *stu3=[student studentwithfirstname::@ "Hitu" lastname:@ "Sharma"];

Student *stu4=[student studentwithfirstname::@ "Madani" lastname:@ "Green"];

Nsarray *array=[nsarray Arraywithobjects:stu1,stu2,stu3,stu4,nil];

Using a block to compare

Nsarray *araray2=[array sortedarrayusingcomparator:^nscomparisonresult (Student *obj1,student *obj2) {

Copy algorithm here...double Click can add a block quickly

First sort with LastName then FirstName

Nscomparisionresult Result=[obj1.lastname Compare:obj2.lastname];

If have same LastName then compare the FirstName

if (result==nsordersame) {

Result=[obj1.firstname Compare:obj2.firstname];

}

return result;

}];

NSLog (@ "array2:%@", array2);

}

32.17

Nsarray Sort 4 Student have a subclass book

Sort first with bookname then LastName then FirstName

#pragma mark Using block to sort

Book.h

@interface Book:nsobject

@property (Nonatomic,retain) NSString *name;

+ (ID) bookwithname: (NSString *) name;

@end

Book.m

@implementation Book

+ (ID) bookwithname: (NSString *name) {

Book *book=[[[book Alloc]init]autorelease];

Book.name=name;

return book;

}

-(void) dealloc{

[_name release];

[Super Dealloc];

}

@end

----------------------

Student.h//add

@class book;

@property (nonatomic,retain) book *book;

+ (ID) studentwithfirstname: (NSString *) FirstName LastName: (NSString *) LastName BookName: (nstring *) bookname;

Student.m//add

+ (ID) studentwithfirstname: (NSString *) FirstName LastName: (NSString *) LastName BookName: (nstring *) bookname{

Student *stu=[student Studentwithfirstname:firstname Lastname:lastname];

Stu.book=[book bookwithname:bookname];//no Memory leak

return Stu;

}

Student.m//add

#import "Book.h"

-(void) dealloc{

[_FirstName release];

[_lastname release];

[_book release];

[Super Dealloc];

}

---------------------

void ArrarSort4 () {

Student *stu1=[student studentwithfirstname::@ "Yesi" bookname:@ "Book1"];

Student *stu2=[student studentwithfirstname::@ "Jujubee" bookname:@ "Book2"];

Student *stu3=[student studentwithfirstname::@ "Hitu" bookname:@ "Book2"];

Student *stu4=[student studentwithfirstname::@ "Madani" bookname:@ "Book1"];

Nsarray *array=[nsarray Arraywithobjects:stu1,stu2,stu3,stu4,nil];

1 Sort by Title first

Nssortdescriptor *booknamedesc=[nssortdescriptor sortdescriptorwithkey:@ "Book.name" Ascending:YES];

2 again by surname

Nssortdescriptor *lastamedesc=[nssortdescriptor sortdescriptorwithkey:@ "LastName" Ascending:yes];

3 Finally by name

Nssortdescriptor *firstnamedesc=[nssortdescriptor sortdescriptorwithkey:@ "FirstName" Ascending:yes];

Nsarray Array2=[array Sortedarrayusingdescriptors:]

}

Nsarray derivation of a new collection

-(Nsarray *) Arraybyaddingobject: (ID) anobject

Add a new Array,and return a new Nsarray

-(Nsarray *) Arraybyaddingobjectsfromarray: (Nsarray *) Anotherarray

Add all the elements Inanother array and return a new Nsarray original array not change

-(Nsarray *) Subarraywithrange: (nsarray) Range

Load elements within range

#pragma derive a new array

void Arraynew () {

Nsarray *array=[nsarray arraywithobjects:@ "1", @ "2", nil];

Nsarray *array2=[array arraybyaddingobject:@ "3"];//merging arrays

The original array did not change, just the return array2 changed.

Nsarray *array3=[array arraybyaddingobjectsfromarray:[nsarray arraywithobject:@ "4", @ "5", NIL]];//4 elements

Nsarray *array4=[array arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil];

Nsrange range= nsmakerange (n);

Nsarray *array5=[array4 Subarraywithrange:range];

}

Other method.

-(NSString) componentsjoinedbystring: (NSString *) seperator

Use a seperator to joint all the elements as a string

-(BOOL) write to File: (NSString *) path atomically: (bool) Use Auxiliaryfile

Tell a nsarray to persist in a file.

#pregma Mark Other method

void Arrayother () {

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

NSString *string=[array componentsjoinedbystring:@ ","];//1,2,3,4

NSString *[email protected] "/users/apple/desktop/array.xml";

[Arry writetofile:path atomically:yes];//create a XML file

Reading array contents from a file (file has strict formatting requirements)

NSString *[email protected] "/users/apple/desktop/array.txt";

Nsarray *array2=[nsarray Arraywithcontentsoffile:path];

}

Nsarray Sort 1

-(Nsarray *) Sortedarrayusingselector: (SEL) Comparator

Return a new sorted array, original array won't change

#pragma Mark ArraySort

void ArraySort1 () {

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

Nsarray *array2=[array sortedarrayusingselector: @selector (compare:)];

[@ "1" compare:@ "2"]; Sort by the incoming comparison method

Default order is from smaller to bigger

}

Nsarray Sort 2

For example, this is called (assuming the array contains a student object);

-(Nsarray *) Sortedarrayusingcomparator: (Comparator) cmptr

typedef nscomoarisionresult (^nscomparator) (id obj1,id obj2);

[Array sortedarrayusingcomparatorresult:^ (Student *s1,student *s2) {

return [S1.name Compare:s2.name;]

}];

Sort by name

Student.h

@interface Student:nsobject

@property (Nonatomic,retain) NSString *firstname;

@property (Nonatomic,retain) NSString *lastname;

+ (ID) studentwithfiestname: (NSString *) FirstName LastName: (*nsstring *) LastName;

Pass to @selector and return value must is Nscomparisionresult

-(Nscomparisionresult) Comparestudent: (Student *) Stu;

@end

Student.m

@implementation

+ (ID) studentwithfiestname: (NSString *) FirstName LastName: (*nsstring *) lastname{

Student *stu=[[[student Alloc]init]autorelease];

Because of static method + so we need to add autorelease.

Stu.lastname=lastname;

Stu.firstname=firstname;

return Stu;

}

We can store LastName Reult first

-(Nscomparisionresult) Comparestudent: (Student *) stu{

First sort with LastName then FirstName

Nscomparisionresult Result=[self.lastname Compare:stu.lastname];

If have same LastName then compare the FirstName

if (result==nsordersame) {

Result=[self.firstname Compare:stu.firstname];

}

return result;

}

}

-(void) dealloc{

[_FirstName release];

[_lastname release];

[Super Dealloc];

}

Rewrite Description Method

Yesi

-(NSString *) description{

return [NSString stringwithformat:@ "%@%@", Self.lastname,self.firstname];

}

@end

-----------------------

#progma Mark Array Sort 2

#import "Student.h"

void ArraySort2 () {

Student *stu1=[student studentwithfirstname::@ "Yesi"];

Student *stu2=[student studentwithfirstname::@ "Jujubee"];

Student *stu3=[student studentwithfirstname::@ "Hitu"];

Student *stu4=[student studentwithfirstname::@ "Madani"];

Nsarray *array=[nsarray Arraywithobjects:stu1,stu2,stu3,stu4,nil];

Now need to sort with FirstName then LastName

Nsarray *array2=[array sortedarrayusingselector: @selctor (comparestudent)];

}

Nsarray Sort 3 Use block (block completely not understand OK, roleback to see again)

#pragma mark Using block to sort

#import "Student.h"

void ArraySort3 () {

Student *stu1=[student studentwithfirstname::@ "Yesi"];

Student *stu2=[student studentwithfirstname::@ "Jujubee"];

Student *stu3=[student studentwithfirstname::@ "Hitu"];

Student *stu4=[student studentwithfirstname::@ "Madani"];

Nsarray *array=[nsarray Arraywithobjects:stu1,stu2,stu3,stu4,nil];

Using a block to compare

[Array Sortedarrayusingcomparator:^nscomparisonresult (Student *obj1,student *obj2) {//copy algorithm here...double Click can add a block quickly

First sort with LastName then FirstName

Nscomparisionresult Result=[obj1.lastname Compare:obj2.lastname];

If have same LastName then compare the FirstName

if (result==nsordersame) {

Result=[obj1.firstname Compare:obj2.firstname];

}

return result;

}];

}

Nsarray Sort 4 Student have a subclass book

Sort first with bookname then LastName then FirstName

#pragma mark Using block to sort

Book.h

@interface Book:nsobject

@property (Nonatomic,retain) NSString *name;

+ (ID) bookwithname: (NSString *) name;

@end

Book.m

@implementation Book

+ (ID) bookwithname: (NSString *name) {

Book *book=[[[book Alloc]init]autorelease];

Book.name=name;

return book;

}

-(void) dealloc{

[_name release];

[Super Dealloc];

}

@end

----------------------

Student.h//add

@class book;

@property (nonatomic,retain) book *book;

+ (ID) studentwithfirstname: (NSString *) FirstName LastName: (NSString *) LastName BookName: (nstring *) bookname;

Student.m//add

+ (ID) studentwithfirstname: (NSString *) FirstName LastName: (NSString *) LastName BookName: (nstring *) bookname{

Student *stu=[student Studentwithfirstname:firstname Lastname:lastname];

Stu.book=[book bookwithname:bookname];//no Memory leak

return Stu;

}

Student.m//add

#import "Book.h"

-(void) dealloc{

[_FirstName release];

[_lastname release];

[_book release];

[Super Dealloc];

}

---------------------

void ArrarSort4 () {

Student *stu1=[student studentwithfirstname::@ "Yesi" bookname:@ "Book1"];

Student *stu2=[student studentwithfirstname::@ "Jujubee" bookname:@ "Book2"];

Student *stu3=[student studentwithfirstname::@ "Hitu" bookname:@ "Book2"];

Student *stu4=[student studentwithfirstname::@ "Madani" bookname:@ "Book1"];

Nsarray *array=[nsarray Arraywithobjects:stu1,stu2,stu3,stu4,nil];

1 Sort by Title first

Nssortdescriptor *booknamedesc=[nssortdescriptor sortdescriptorwithkey:@ "Book.name" Ascending:YES];

2 again by surname

Nssortdescriptor *lastamedesc=[nssortdescriptor sortdescriptorwithkey:@ "LastName" Ascending:yes];

3 Finally by name

Nssortdescriptor *firstnamedesc=[nssortdescriptor sortdescriptorwithkey:@ "FirstName" Ascending:yes];

Nsarray Array2=[array Sortedarrayusingdescriptors:]

}

Sort of Nsarray

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.