1, Array type elements are of type NSNumber, directly sort the elements of array type:

#pragma mark - (void) arraySortAsc {NSArray * Array = @[@(3),@(4),@(2),@(1)]; / / prioritize NSArray * result = array (array sortedArrayUsingComparator: ^ NSComparisonResult (id _Nonnull obj1, Id _Nonnull obj2) {NSLog (@ "% @ ~ % @", obj1, obj2); / / 3 ~ 4 2 ~ 3 to 1 ~ 2 return [obj1 compare: obj2]; / / ascending}]; NSLog(@"result=%@",result); } #pragma mark - (void) arraySortDesc {NSArray * Array = @[@(3),@(4),@(2),@(1)]; / / prioritize NSArray * result = array (array sortedArrayUsingComparator: ^ NSComparisonResult (id _Nonnull obj1, Id _Nonnull obj2) {NSLog (@ "% @ ~ % @", obj1, obj2); / / 3 ~ 4 2 ~ 3 to 1 ~ 2 return [obj2 compare: obj1); / / descending}]; NSLog(@"result=%@",result); } #pragma mark - (void)arraySortBreak{NSArray * Array = pragma mark @ @ (3), @ (4), @ (2), @ (1), @ (5), @ (6), @ (0)]; / / prioritize NSArray * result = array (array sortedArrayUsingComparator: ^ NSComparisonResult (id _Nonnull obj1, If (Arc4Random_Uniform (2) == 0) {return [obj2 compare:obj1]; Else {return [obj1 compare:obj2];}}]; NSLog(@"result=%@",result); }

2. The elements in the array are objects, sorted according to an attribute of the object:

#pragma mark -- (void) arraySortWithKind :(KindType) KindType {// Array * Array = [NSArray arrayWithArray:self.dataMuArr]; / / prioritize NSArray * result = array (array sortedArrayUsingComparator: ^ NSComparisonResult (id _Nonnull obj1, id _Nonnull obj2) { NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2 if ([obj1 isKindOfClass:[BATicker class]] && [obj2 isKindOfClass:[BATicker class]]) { BATicker * ticker1 = obj1; BATicker * ticker2 = obj2; NSNumber * number1 = @(ticker1.c); NSNumber * Number2 = @(ticker2.c); if (KindType == Kindtypeturnover) {// Ntivernover = @(ticker1.q); }else if (KindType == KindTypePrice) {// Number1 = @(ticker1.c); number2 = @(ticker2.c);}else if (KindType == KindTypePrice) {// Number2 = @(ticker2.c) If (KindType == KindTypePercentageGain) {return Number1 = @(ticker1.price_p); Number2 = @(ticker2.price_p);} return // return [obj1 compare:obj2]; // return [obj1 compare:obj2]; NSMutableDictionary * keyIndexDic = [NSMutableDictionary dictionary]; for (int i = 0; i < result.count; i++) { BATicker * ticker = result[i]; [keyIndexDic setObject:@(i) forKey:ticker.s]; } self.dataMuArr = [NSMutableArray arrayWithArray:result]; self.keyIndexDic = [NSMutableDictionary dictionaryWithDictionary:keyIndexDic]; [self.tableView reloadData]; NSLog(@"result=%@",result); } #pragma mark - (void) arraySortDescKind :(KindType) KindType {NSArray * Array = [NSArray arrayWithArray:self.dataMuArr]; / / prioritize NSArray * result = array (array sortedArrayUsingComparator: ^ NSComparisonResult (id _Nonnull obj1, id _Nonnull obj2) { NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2 if ([obj1 isKindOfClass:[BATicker class]] && [obj2 isKindOfClass:[BATicker class]]) { BATicker * ticker1 = obj1; BATicker * ticker2 = obj2; NSNumber * number1 = @(ticker1.c); NSNumber * Number2 = @(ticker2.c); if (KindType == Kindtypeturnover) {// Ntivernover = @(ticker1.q); }else if (KindType == KindTypePrice) {// Number1 = @(ticker1.c); number2 = @(ticker2.c);}else if (KindType == KindTypePrice) {// Number2 = @(ticker2.c) If (KindType == KindTypePercentageGain) {return Number1 = @(ticker1.price_p); Number2 = @(ticker2.price_p);} return Return [obj2 compare:obj1]; // return [obj2 compare:obj1]; NSMutableDictionary * keyIndexDic = [NSMutableDictionary dictionary]; for (int i = 0; i < result.count; i++) { BATicker * ticker = result[i]; [keyIndexDic setObject:@(i) forKey:ticker.s]; } self.dataMuArr = [NSMutableArray arrayWithArray:result]; self.keyIndexDic = [NSMutableDictionary dictionaryWithDictionary:keyIndexDic]; [self.tableView reloadData]; NSLog(@"result=%@",result); }

Array of reference content: iOS sort (ascending, descending and out-of-order) [https://www.cnblogs.com/xiao-…