Objective C - Sort an array of string

后端 未结 3 1446
长发绾君心
长发绾君心 2021-01-03 22:43

I have to sort an array of objects by a property of the objects that is a string. How can I do this?

3条回答
  •  情歌与酒
    2021-01-03 23:03

    For just sorting array of strings:

    sorted = [array sortedArrayUsingSelector:@selector(compare:)];
    

    For sorting objects with key "name":

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(compare:)];
    sorted = [array sortedArrayUsingDescriptors:@[sort]];
    

    Also, instead of compare: you can use:

    • caseInsensitiveCompare:

    • localizedCaseInsensitiveCompare:

提交回复
热议问题