Objective-C: How to sort NSArray Object in ascending order?

NSArray *arrayOfExamples = @[@{@"title": "hello world", @"message": "This is an example"},                          @{"title": "hellow water", @"message": "This is another example"}];

NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];

NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];

NSArray *sortedArray = [arrayOfExamples sortedArrayUsingDescriptors:sortDescriptors];
  • arrayOfExamples is created, which is an NSArray object. It contains two NSDictionary objects in it.
  • sortByName is created, which is an NSSortDescriptor. We described the key to be used to sort the NSArray object and in which order it needs to be sorted.
  • sortDescriptors is created, which contains the descriptor sortByName.
  • sortedArray is a new NSArray to store the sorted NSArray object. It is sorted using sortDescriptors

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akarsh SEGGEMU

Akarsh SEGGEMU

Software Engineer (Android, iOS, Unity) | M.Sc. Computer Science — Technische Universitaet Berlin | Mobile Advertising, Attribution, Monetisation, UA & Analytic