Question

I have to sort an arrray according to configuration , Suppose its has some data of student which has student name

    [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"abc",@"name", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"efg",@"name", nil][NSDictionary dictionaryWithObjectsAndKeys:@"cde",@"name", nil], nil];

and i have another array which say how to sort this array like its say first should be efg then abc and so on.

Is it possible using nsssortdescriptor or i have to write my custom code.

Was it helpful?

Solution

I would use a block to allow you to specify your comparison logic

NSArray *sortedArray = [_helpItems sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    return [(MAHelpItem *)a order] > [(MAHelpItem *)b order];
}];
self.items = sortedArray;

OTHER TIPS

Perhaps this is what you're looking for:

  NSArray *students;
  NSArray *sortPriorities;
  NSMutableArray *sortDescriptors = @[].mutableCopy;
  for (NSString *key in sortPriorities) {
    [sortDescriptors addObject:[NSSortDescriptor sortDescriptorWithKey:key ascending:YES]];
  }
  NSArray *sortedStudents = [students sortedArrayUsingDescriptors:sortDescriptors];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top