ZYArray.h
#import <Foundation/Foundation.h>typedefBOOL (^SortArray)(char *countries1,char *countries2);@interface ZYArray : NSObject- (void)sortWithArray:(char *[])array andLength:(int)length andBlcok:(SortArray)sortArray;@end
Zyarray.m
#import"ZYArray.h"@implementation zyarray-(void) Sortwitharray: (Char*[])ArrayAndlength: (int) Length Andblcok: (Sortarray) sortarray{ for(inti =0; I<length-1; i++) { for(intj =0; J<length-i-1; J + +) {BOOL res = Sortarray (Array[j],Array[j+1]);if(Res >0) {Char*temp =Array[j];Array[j] =Array[j+1];Array[j+1] = temp; } } } for(inti =0; I<length; i++) {NSLog (@'%s ',Array[i]); }}@End
Main.m
#import <Foundation/Foundation.h> #import "ZYArray.h" intMainintargcConst Char* argv[]) {Char*countries[] = {"Nepal","Cambodia","Afghanistan","China","Singapore","Bangladesh","India","Maldives","South Korea","Bhutan","Japan","Sikkim","Sri Lanka","Burma","North Korea","Laos","Malaysia","Indonesia","Turkey","Mongolia","Pakistan","Philippines","Vietnam","Palestine"};intLength =sizeof(countries)/sizeof(countries[0]); Zyarray *array = [[Zyarray alloc]init]; [Array sortwitharray:countries andlength:length andblcok:^BOOL(Char*countries1,Char*countries2) {returnstrcmp (Countries1, countries2); }];return 0;}
This is sorted alphabetically and of course it can be sorted in other ways. See what you need
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Block and method sort the names of each country