How to sort NSComboBox contents

280 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 answers

1
Anoop Vaidya 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
copydigital 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.