Sorting mutable array in ascending order in Objective-C

Total Post:30

 1398  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.

  1. Post:214

    Re: Sorting mutable array in ascending order in Objective-C

    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



Enter your email address here always to be updated. We promise not to spam!