帮助跨两个属性对NSArray进行排序(使用NSSortDescriptor?) [英] Help sorting an NSArray across two properties (with NSSortDescriptor?)

查看:94
本文介绍了帮助跨两个属性对NSArray进行排序(使用NSSortDescriptor?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点NSSortDescriptor n00b.不过,我认为这是我需要做的正确工具:

I'm a bit of a NSSortDescriptor n00b. I think, though, it is the right tool for what I need to do:

我有一个NSArray,其中包含带有键的对象,例如名称"和时间".而不是说出它,这是一个示例:

I have an NSArray consisting of objects with keys, say, "name" and "time". Instead of verbalizing it, here's an example:

input:

name: time
B: 4
C: 8
B: 5
C: 4
A: 3
C: 2
A: 1
A: 7
B: 6


desired output:

name: time
A: 1 <---
A: 3
A: 7
C: 2 <---
C: 4
C: 8
B: 4 <---
B: 5
B: 6

因此,这些值按时间"排序,并按名称"分组. A排在第一位,因为他的时间值最小,A的所有值都紧随其后.然后是C,他的时间价值是所有时间价值中第二小的.我已经指出了确定名称排序方式的值.在每个名称组中,按时间排序.

So the values are sorted by "time" and grouped by "name". A comes first because he had the smallest time value, and all values for A come after one another. Then comes C, he had the second smallest time value out of all his values. I have indicated the values that determine how the names are sorted; within each name group, sorting is by time.

如何以最有效的方式从输入到输出NSArray? (从CPU和内存角度来看,不一定是从代码角度来看.)我将如何为此构造NSSortDescriptors或使用任何其他方法?除非这是最有效的方法,否则我不想自己动手.

How do I get from input to output NSArray in the most efficient way? (cpu- and memory-wise, not necessarily code-wise.) How would I construct the NSSortDescriptors for this, or use any other method? I don't want to roll my own unless it's the most efficient way.

推荐答案

The sortedArrayUsingDescriptors: NSArray method does most of what you need:

第一个描述符指定用于对接收者的内容进行排序的主键路径.任何后续的描述符都用于进一步优化具有重复值的对象的排序.有关其他信息,请参见NSSortDescriptor.

The first descriptor specifies the primary key path to be used in sorting the receiver’s contents. Any subsequent descriptors are used to further refine sorting of objects with duplicate values. See NSSortDescriptor for additional information.

还需要使用NSPredicate进行一些过滤:

Some filtering with NSPredicate is required too:

NSSortDescriptor *timeSD = [NSSortDescriptor sortDescriptorWithKey: @"time" ascending: YES];

NSMutableArray *sortedByTime = [UnsortedArray sortedArrayUsingDescriptors: timeSD];
NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:[sortedByTime count]];

while([sortedByTime count]) 
{
        id groupLead = [sortedByTime objectAtIndex:0];  
        NSPredicate *groupPredicate = [NSPredicate predicateWithFormat:@"name = %@", [groupLead name]];

        NSArray *group = [sortedByTime filteredArrayUsingPredicate: groupPredicate];

        [sortedArray addObjectsFromArray:group];
        [sortedByTime removeObjectsInArray:group];
}

我不知道这是否是最有效的方法,但是直到您有理由相信这会导致问题时,才有必要担心性​​能问题.这是过早的优化.我不会担心这种方法的性能.您必须信任该框架,否则由于毫无根据的妄想症,最终将对其进行重写(因此破坏了框架的要点).

I have no idea if this is the most efficient method, but until you have reason to believe that it is causing problems there's no need to worry the performance implications. It's premature optimisation. I wouldn't have any concerns about the performance of this method. You've got to trust the framework otherwise you'll end up rewriting it (thus undermine the point of the framework) due to an unfounded paranoia.

这篇关于帮助跨两个属性对NSArray进行排序(使用NSSortDescriptor?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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