English 中文(简体)
排序存储在 NANSrray 中的密钥/ 价值
原标题:Sorting the Key/Values stored in NSArray

我有一个

NSDictionary* dict = [[NSDictionary alloc]initWithObjectsAndKeys::arrayOne,@"Plants",arrayTwo,@"Animals"),arrayThree,@"Birds",nil];`

self.displayArray =[[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];

一切正常,我都能在桌子上看到所有关键价值/价值,但是它们是按照分类顺序排列的,如动物、鸟、普朗茨。

但我想以植物、动物、鸟等形式展示

Can anyone tell me how to sort the array in my customized order? I have googled and found that we can use NSSortDescriptor for sorting. But I am not very clear with that. Can anyone help me ?

最佳回答

由于您的订单不遵循任何自然顺序,一个简单的解决办法可能是用另一个阵列来跟踪订单的顺序。

NSArray *array1 = [NSArray arrayWithObjects:@"rose",@"orchid",@"sunflower",nil];
NSArray *array2 = [NSArray arrayWithObjects:@"dog", @"cat",@"ogre",@"wookie", nil];
NSArray *array3 = [NSArray arrayWithObjects:@"parrot",@"canary bird",@"tweety",@"bibo",nil];


NSArray *keys = [NSArray arrayWithObjects:@"Plants",@"Animals",@"Birds", nil];

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    array1,[keys objectAtIndex:0],
                                    array2,[keys objectAtIndex:1], 
                                    array3,[keys objectAtIndex:2], 
                          nil];

for (NSString *key in keys) {
    NSLog(@"%@ %@", key, [dict objectForKey:key]);
}

Matt在他的精彩博客中展示了如何创建有顺序的字典, 基本上使用其他阵列来维持顺序, 正如我在这里所显示的: orddered Dictionary:

问题回答

你走对了,西里尔

以下是一些"http:// developer.apple.com/library/mac/#documentation/Cocoa/Ceptitual/Sort Descriptionors/Concepties/Creting.html" rel=“nofollow”>关于“创建和使用排序描述”的文件

基本上,您需要下级NSSSort Descriptionor, 在您的子类中, 执行您自己的“ 比较 : ” 方法( 您可以实际命名任何您想要的东西; 它需要返回一个“ code> NSComparisonResult ), 在“ 动物” 之前, 顺理成章地返回“ Plants ” 。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签