Friday, September 18, 2015

Objective C NSSortDescriptor

Objective C sort descriptors are very useful for sort options on a list in our applications. 

For example, we got a list of employees from server and we need to display it in a list including sort options like name, salary, experience of the employees.

For that, I am creating employee class for employee information as below.


@interface employee : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *employeeID;
@property (nonatomic, assign) int salary;
@property (nonatomic, assign) int totalExperience;
@property (nonatomic, assign) int yearOfJoining;
@end 

As we need to display all the employees in a table view, I am taking an array and adding all the employee objects to it as shown below.


NSMutableArray *employees = [[NSMutableArray alloc] init];
    
    employee *emp1 = [[employee alloc] init];
    [emp1 setName:@"john"];
    [emp1 setEmployeeID:@"101"];
    [emp1 setSalary:12000];
    [emp1 setTotalExperience:5];
    [emp1 setYearOfJoining:2000];
    
    employee *emp2 = [[employee alloc] init];
    [emp2 setName:@"smith"];
    [emp2 setEmployeeID:@"102"];
    [emp2 setSalary:18000];
    [emp2 setTotalExperience:8];
    [emp2 setYearOfJoining:2003];
    
    employee *emp3 = [[employee alloc] init];
    [emp3 setName:@"paul"];
    [emp3 setEmployeeID:@"103"];
    [emp3 setSalary:8000];
    [emp3 setTotalExperience:2];
    [emp3 setYearOfJoining:2001];
    
[employees addObject:emp1];[employees addObject:emp2];[employees addObject:emp3];

I will use this employees array for displaying employees in a table view. 

What If I want to sort employees by name? As per the addition of employee objects, the order of employees display will be as shown below. 

NSLog(@"Before : %@",[employees valueForKeyPath:@"name"]);

Before : (
    john,
    smith,
    paul
)

Obviously, this is not in alphabetical order. Let's see how we can sort the names using NSSortDescriptor.

NSSortDescriptor *sortDescriptor = [NSSortDescriptor
                                                           sortDescriptorWithKey:@"name"
                                                           ascending:YES];

NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
employees = [[employees sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];

NSLog(@"After : %@",[employees valueForKeyPath:@"name"]);

After : (
    john,
    paul,
    smith
)

Looks so simple right? Simple but powerful class for all the sort options. If you observe, I have given the key as name which is the property of employees class and applied that sort descriptor on the employees array. We can choose sort order also by playing with the ascending property.

Similarly we can apply sort on salary and experience of the employees also.

NSSortDescriptor *sortDescriptor = [NSSortDescriptor
                                                           sortDescriptorWithKey:@"salary"
                                                           ascending:YES];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor
                                                           sortDescriptorWithKey:@"totalExperience"
                                                           ascending:YES];



Thanks for reading this article. Hope you found it useful :)