在objective-c中对多维数组进行排序 [英] Sorting a multidimensional array in objective-c

查看:98
本文介绍了在objective-c中对多维数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在objective-c中对多维数组进行排序我知道我可以使用下面的代码行对单维数组进行排序:

I'm trying to sort a multidimensional array in objective-c i know that i can sort a single dimensional array using the line of code below:

NSArray *sortedArray = [someArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

我似乎无法弄清楚如何对2D数组进行排序,如下所示:

I can't seem to figure out how to sort a 2D array like the one below:

 ( ("SOME_URL", "SOME_STORY_TITLE", "SOME_CATEGORY"),
    ("SOME_URL", "SOME_STORY_TITLE", "SOME_CATEGORY"),
    ("SOME_URL", "SOME_STORY_TITLE", "SOME_CATEGORY") );

如果有人可以提供我将通过SOME_CATEGORY排序数组的代码,那对我来说会有很大的帮助。

If someone could provide me code that would sort the array by SOME_CATEGORY it would be of great help to me.

谢谢,

Zen_Silence

Zen_Silence

推荐答案

你需要使用 -sortedArrayUsingFunction:context: sortedArrayUsingFunction:context:提示:

static NSInteger order (id a, id b, void* context) {
    NSString* catA = [a lastObject];
    NSString* catB = [b lastObject];
    return [catA caseInsensitiveCompare:catB];
}
...
NSArray* sortedArray = [someArray sortedArrayUsingFunction:order context:NULL];

或者,为内部数组创建一个自定义类(更像是一个记录),并在其上实现 -compareByCategory:方法。

Alternatively, create a custom class for your inner "array" (which is more like a record), and implement a -compareByCategory: method on it.

这篇关于在objective-c中对多维数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆