Objective C - Sort an array of string

后端 未结 3 1416
长发绾君心
长发绾君心 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条回答
  •  -上瘾入骨i
    2021-01-03 23:18

    Here's what I ended up using - works like a charm:

    [categoryArray sortedArrayWithOptions:0
                   usingComparator:^NSComparisonResult(id obj1, id obj2)
    {
        id cat1 = obj1;
        id cat2 = obj2;
        return [cat1.name compare:cat2.name options:NSCaseInsensitiveSearch];
    }];
    

    SelectableCategory is just a @protocol SelectableCategory defining the category with all its properties and elements.

提交回复
热议问题