目标C -对字符串数组进行排序

时间:2022-11-24 15:58:20

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

我必须根据对象的属性(字符串)对对象数组进行排序。我该怎么做呢?

3 个解决方案

#1


35  

you need to use

您需要使用

-[NSArray sortedArrayUsingSelector:]

or

-[NSMutableArray sortUsingSelector:] and pass @selector(compare:) as the parameter.

-[NSMutableArray sortUsingSelector:]并将@selector(compare:)作为参数。

here's a link to the answer Sort NSArray of date strings or objects

这里有一个指向日期字符串或对象的答案排序NSArray的链接

#2


5  

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:

    caseInsensitiveCompare:

  • localizedCaseInsensitiveCompare:

    localizedCaseInsensitiveCompare:

#3


3  

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

这是我最后使用的;就像一个魅力:

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

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

SelectableCategory只是一个@protocol SelectableCategory ,定义具有所有属性和元素的类别。

#1


35  

you need to use

您需要使用

-[NSArray sortedArrayUsingSelector:]

or

-[NSMutableArray sortUsingSelector:] and pass @selector(compare:) as the parameter.

-[NSMutableArray sortUsingSelector:]并将@selector(compare:)作为参数。

here's a link to the answer Sort NSArray of date strings or objects

这里有一个指向日期字符串或对象的答案排序NSArray的链接

#2


5  

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:

    caseInsensitiveCompare:

  • localizedCaseInsensitiveCompare:

    localizedCaseInsensitiveCompare:

#3


3  

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

这是我最后使用的;就像一个魅力:

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

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

SelectableCategory只是一个@protocol SelectableCategory ,定义具有所有属性和元素的类别。