按另一个数组内容对NSMutableArray进行排序

时间:2014-08-10 14:37:29

标签: objective-c arrays sorting nsmutablearray nsarray

我正在尝试通过NSArray内容对NSMutableArray进行排序。

例如,NSMutableArray(包含NSMutableDictionnary)的内容是:

{
    detail = "Al's legend text";
    image = "http://...";
    main = "Al's main text;
    section = "United States";
},
{
    detail = "Yumi's legend text";
    image = "http://...";
    main = "Yumi's main text;
    section = "Japan";
},
{
    detail = "Enrico's legend text";
    image = "http://...";
    main = "Enrico's main text;
    section = "Spain";
}, ...

目前,我知道如何按键,升序或降序对其进行排序,但我需要的是NSMutableArray按以下NSArray内容排序:

{ @"Japan",@"France",@"United States",@"Uruguay",@"Spain", ...}

如果有人可以帮我弄清楚。非常感谢!

1 个答案:

答案 0 :(得分:0)

您想使用sortUsingSelector:nsmutablearray数组的这种方法要求您编写一个执行自定义比较的函数。查看文档,它将向您展示如何使用它。

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/sortUsingSelector