Search code examples
objective-cxcodemacosnsmutablearraynscombobox

How to sort NSComboBox contents


Here is my code to display combo box:

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

Solution

  • 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];