Я сделал следующий код, так что, думаю, я ответил на свой вопрос:
-(void)sortMenu:(NSMenu*)menu
{
// [CH] Get an array of all menu items.
NSArray* items = [menu itemArray];
[menu removeAllItems];
// [CH] Sort the array
NSSortDescriptor* alphaDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
NSSortDescriptor* submenuDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"hasSubmenu" ascending:NO];
items = [items sortedArrayUsingDescriptors:[NSArray arrayWithObjects:submenuDescriptor,alphaDescriptor, nil]];
// [CH] ok, now set it back.
for(NSMenuItem* item in items){
[menu addItem:item];
/**
* [CH] The following code fixes NSPopUpButton's confusion that occurs when
* we sort this list. NSPopUpButton listens to the NSMenu's add notifications
* and hides the first item. Sorting this blows it up.
**/
if(item.isHidden){
item.hidden = false;
}
// [CH] While we're looping, if there's a submenu, go ahead and sort that, too.
if(item.hasSubmenu){
[self sortMenu:item.submenu];
}
}
}