Home > DeveloperSection > Forums > Sorting mutable array in ascending order in Objective-C
David Miller

Total Post:30

Posted on    December-07-2015 10:41 PM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 839  View(s)
Rate this:

I have created a studentDB table in a SQLite database and storing the data of studentDB in an NSMutableArray.
I have created attributes in our studentDB main attributs are as follows: Student_Name, Registration_No, Date_of_Join.

Now, I want to sort this mutable array according to Student_Name in ascending order in a Table View.

So, how can I do this. any help would be appreciated.

Tarun Kumar

Total Post:214

Posted on    December-08-2015 11:04 PM

For sorting arrays use NSSortDescriptor's method sortUsingDescriptors:sortDescriptors to sort a mutable array.
for Ex:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc]

initWithKey:@"Date_Of_Join" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
[array sortUsingDescriptors:sortDescriptors];
[descriptor release];

We can sort using different methods:
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr

Don't want to miss updates? Please click the below button!

Follow MindStick