Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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;
share|improve this answer

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];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.