Since I wrote the use of the array of sorting, found this article is good, recommend to everyone.
Article from: http://blog.csdn.net/daiyelang/article/details/18726947
// Create an empty array nsarray *array = [Nsarray array]; // Create an array of 1 elements array = [Nsarray arraywithobject:@" 123 " // Create an array with multiple elements array = [Nsarray arraywithobjects:@" a ", @" B ", @" C " *array3 = [Array Arraybyaddingobjectsfromarray:[nsarray arraywithobjects:@ "4" @ "5" *array4 = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4" = Nsmakerange (1, 2 *array5 = [Array4 subarraywithrange:range];
2. Some basic methods of arrays
intCount = [array count];//number//determine if an element is includedif([Array containsobject:@ "a"]) {NSLog (@"Contains string a"); } nsstring*last =[Array Lastobject]; the last element NSString*STR = [Array objectatindex:1]; Gets the elements in the array according to the indexintindex = [array indexofobject:@ "C"]; Gets the index of the specified element//let all objects inside the array call the test method, 123 is the parameterNsarray *array =[Nsarray arraywithobjects:stu1, STU2, STU3, nil Nil]; [Array makeobjectsperformselector: @selector (test2:) withobject:@"123"]; Nsarray*array = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil Nil]; //1-2-3-4//use separators-to stitch all array elementsNSString *str = [Array componentsjoinedbystring:@ "-"]; //writes an array to a file (an XML file is generated)NSString *path = @ "/users/apple/desktop/array.xml"; [Array Writetofile:path atomically:yes]; Path= @ "/users/apple/desktop/array.txt"; //reading array contents from a file (file has strict formatting requirements)Nsarray *array2 = [Nsarray Arraywithcontentsoffile:path];
3. Iterating through an array
#pragma mark iterates through the array 1voidArrayFor1 () {Nsarray*array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil]; intCount =Array.count; for(inti = 0; i<count; i++) {ID obj=[Array objectatindex:i]; }} #pragma mark iterates through the array 2 fast traversalvoidArrayFor2 () {Student*STU1 =[Student Student]; Nsarray*array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil]; intI =0; for(ID objinchArray) {NSLog (@"%i-%@", I, obj); I++; }} #pragma mark iterates through the array 3voidArrayFor3 () {Student*STU1 =[Student Student]; Nsarray*array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil]; [Array Enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, Boolbool *stop) {NSLog (@"%i-%@", idx, obj); //if the index is 1, stop traversing if(idx = = 1) { //using pointers to modify the value of a bool variable outside*stop =YES; } }]; } #pragma mark iterates through the array 4voidArrayFor4 () {Student*STU1 =[Student Student]; Nsarray*array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil]; //get an iterator to an array //nsenumerator *enumerator = [array objectenumerator]; //Reverse-order iterator (traversing elements from the tail)Nsenumerator *enumerator =[Array reverseobjectenumerator]; //allobjects is to remove objects that have not been traversedNsarray *array2 =[Enumerator allobjects]; NSLog (@"Array2:%@", array2); //gets the next element to traverseID obj =Nil; while(obj =[Enumerator Nextobject]) {NSLog (@"Obj=%@", obj); } }
4. Array sorting
#pragma mark Array sort 1voidArraySort1 () {Nsarray*array = [Nsarray arraywithobjects:@ "2", @ "3", @ "1", @ "4", nil Nil]; //returns an ordered array that does not change the order of the elements in the original array //The comparison method for the specified element: compare:Nsarray *array2 =[Array sortedarrayusingselector: @selector (compare:)]; NSLog (@"Array2:%@", array2); } #pragma mark Array sort 2voidArraySort2 () {Student*STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li"]; Student*STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang"]; Student*STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li"]; Student*stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao"]; Nsarray*array =[Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil]; //Specify a comparison method for sortingNsarray *array2 =[Array sortedarrayusingselector: @selector (comparestudent:)]; NSLog (@"Array2:%@", array2); } -(Nscomparisonresult) Comparestudent: (Student *) Stu {//Sort by last name firstNscomparisonresult result =[Self.lastname Compare:stu.lastname]; //If you have the same last name, compare the names . if(Result = =nsorderedsame) {Result=[Self.firstname Compare:stu.firstname]; } returnresult; } #pragma mark Array sort 3voidArraySort3 () {Student*STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li"]; Student*STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang"]; Student*STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li"]; Student*stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao"]; Nsarray*array =[Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil]; //Sort by blockNsarray *array2 =[Array sortedarrayusingcomparator:^nscomparisonresult (Student *obj1, Student *obj2) { //Sort by last name firstNscomparisonresult result =[Obj1.lastname Compare:obj2.lastname]; //If you have the same last name, compare the names . if(Result = =nsorderedsame) {Result=[Obj1.firstname Compare:obj2.firstname]; } returnresult; }]; NSLog (@"Array2:%@", array2); } #pragma mark Array sort 4-Advanced SortingvoidArraySort4 () {Student*STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li" bookname:@ "Book1"]; Student*STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang" bookname:@ "Book2"]; Student*STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li" bookname:@ "Book2"]; Student*stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao" bookname:@ "Book1"]; Nsarray*array =[Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil]; //1. Sort by Title first //The key here is the name of @property.Nssortdescriptor *booknamedesc = [Nssortdescriptor sortdescriptorwithkey:@ "Book.name"Ascending:yes]; //2. Sort by last nameNssortdescriptor *lastnamedesc = [Nssortdescriptor sortdescriptorwithkey:@ "LastName"Ascending:yes]; //3. Sort by name againNssortdescriptor *firstnamedesc = [nssortdescriptor sortdescriptorwithkey:@ "FirstName"Ascending:yes]; //Add a sort descriptor sequentiallyNsarray *descs =[Nsarray Arraywithobjects:booknamedesc, Lastnamedesc, Firstnamedesc, nil Nil]; Nsarray*array2 =[Array Sortedarrayusingdescriptors:descs]; NSLog (@"Array2:%@", array2); }
iOS array basic usage and sorting Daquan