Xcode how to sort array Object

NSMutableArray *array = [[NSMutableArray alloc] init];
        [array addObject:[[Student alloc] initWithID:1 withName:@"Chi kheang"]];
        [array addObject:[[Student alloc] initWithID:3 withName:@"Rabee"]];
        [array addObject:[[Student alloc] initWithID:2 withName:@"Sothearoth"]];
        [array addObject:[[Student alloc] initWithID:4 withName:@"Bebe"]];
        [array addObject:[[Student alloc] initWithID:5 withName:@"Lyhour"]];
        
        //sort the object
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
        [array sortUsingDescriptors:[NSArray arrayWithObject:sort]];
        
        //print log
        for (Student *s in array) {
            NSLog(@"Student ID = %i and Name = %@\n",s.studentID,s.name);
        }


This is the out put log

0 comments:

Post a Comment