SORTING MUTABLE ARRAY IN ASCENDING ORDER IN OBJECTIVE-C

David Miller

Total Post:30

Points:210
Posted by  David Miller
 1051  View(s)
Ratings:
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. Tarun Kumar

    Post:214

    Points:1642
    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

Answer

NEWSLETTER

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