Я хочу сортировать NSMutableArray в алфавитном порядке.
Как я могу сортировать NSMutableArray в алфавитном порядке?
Ответ 1
Вы можете сделать это, чтобы отсортировать NSMutableArray:
[yourArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Ответ 2
В других ответах, приведенных здесь, упоминается использование @selector (localizedCaseInsensitiveCompare:)
Это отлично работает для массива NSString, однако OP прокомментировал, что массив содержит объекты и что сортировка должна выполняться в соответствии с свойством object.name.
В этом случае вы должны сделать это:
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];
Ваши объекты будут отсортированы в соответствии с свойством имени этих объектов.
Ответ 3
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; // Describe the Key value using which you want to sort.
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor]; // Add the value of the descriptor to array.
sortedArrayWithName = [yourDataArray sortedArrayUsingDescriptors:descriptors]; // Now Sort the Array using descriptor.
Здесь вы получите список отсортированных массивов.
Ответ 4
Используйте класс NSSortDescriptor
, и вы получите все здесь
Ответ 5
NSSortDescriptor * sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name_your_key_value" ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray * sortedArray;
sortedArray = [Your_array sortedArrayUsingDescriptors:sortDescriptors];
Ответ 6
Возможно, это может вам помочь:
[myNSMutableArray sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES],[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES]]];
Все зависит от NSSortDescriptor...