سؤال

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.

هل كانت مفيدة؟

المحلول

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;

نصائح أخرى

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];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top