How to sort NSComboBox contents

303 Views Asked by At

Here is my code to display combo box:

self.records = [[NSMutableArray alloc]
                      initWithArray:[mylist componentsSeparatedByString:@","]];
[self.recordsCombo addItemsWithObjectValues:self.records];
2

There are 2 best solutions below

1
On BEST ANSWER

You never sort comboBox's items. In fact you sort the array, which is the data source for the combo box.

As in your case you need to sort the self.records and then addItems to combobox.

self.records = [[NSMutableArray alloc]
                  initWithArray:[mylist componentsSeparatedByString:@","]];

self.records = [self.records sortedArrayUsingSelector:@selector(compare:)];

[self.recordsCombo addItemsWithObjectValues:self.records];
0
On

Actually sorting alphabetically is covered already here: Sorting Array alphabetically

Otherwise you could alway implement your own sorting algorithm, like Quicksort or somewhat like this. Depends on your skills and needs.