SoFunction
Updated on 2025-04-14

iOS instance code to sort arrays

1. Code.

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  //Sort objects directly  NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];
  NSArray *descriptors = [NSArray arrayWithObject:descriptor];
  NSArray *myDataArray = [NSArray arrayWithObjects:@"what", @"xero", @"highligth", @"mountain", @"Victory", @"Balance", nil];
  NSLog(@"---myDataArray---%@",myDataArray);
  NSArray *resultArray = [myDataArray sortedArrayUsingDescriptors:descriptors];
  NSLog(@"%@", resultArray);
  //NSArray uses sortedArrayUsingDescriptors to return the sorted array.  //NSMutableArray can directly use sortUsingDescriptors to sort the array itself.}

2. Output.

2015-10-23 11:56:45.178 Sort arrays[6739:168615] (
  Balance,
  Victory,
  highligth,
  mountain,
  what,
  xero
)

Summarize

The above is the example code for sorting arrays on iOS introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!