简体   繁体   English

在两个属性上对NSArray进行排序-int和date

[英]Sorting NSArray on two properties - int and date

note from N00B land again. N00B的音符再次降落。 I have read lots about sorting arrays - wanted to try the block method, but haven't wrapped my head around it. 我已经阅读了很多有关对数组进行排序的信息-想尝试使用block方法,但是并没有将其包裹住。 Instead, I opted for the descriptors method. 相反,我选择了描述符方法。 I read this Sort NSArray of custom objects by their NSDate properties and this How to sort an NSMutableArray with custom objects in it? 我阅读了按自定义对象的NSDate属性对NSArray排序的方法,以及如何对其中包含自定义对象的NSMutableArray进行排序的方法? amongst oodles and oodles of others. 在面条和其他面条之间。 In my code I did this: 在我的代码中,我这样做:

NSString *lastHighScore = @"_highScore";
NSString *dateScoreCreated = @"_dateCreated";
NSSortDescriptor *highScoreDescriptor = [[[NSSortDescriptor alloc]
    initWithKey:lastHighScore                                        
    ascending:NO                                          
    selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];

NSSortDescriptor *dateScoreCreatedDescriptor = [[[NSSortDescriptor alloc]
                                      initWithKey:dateScoreCreated
                                      ascending:NO
    selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *descriptors = [NSArray arrayWithObjects:highScoreDescriptor, 
dateScoreCreatedDescriptor, nil];

NSArray *sortedArray = [[[FlipHighScoreStore sharedStore] allHighScores] sortedArrayUsingDescriptors:descriptors];

Sadly, I am getting an error to begin with - initializer element is not a compile-time element. 可悲的是,我开始时遇到了一个错误-初始化元素不是编译时元素。 I looked this up and tried setting NSSortDescriptor *highScoreDescriptor = nil but then I get a warning saying that highScoreDescriptor "Type Specifier Missing, default to int" which in this case is ok, but is not so ok for the Date object in the next descriptor. NSSortDescriptor *highScoreDescriptor = nil了一下,尝试设置NSSortDescriptor *highScoreDescriptor = nil但是然后我收到一条警告,说highScoreDescriptor“类型指定符缺失,默认为int”,在这种情况下还可以,但对于下一个描述符中的Date对象来说就不太好。 (Turns out I am also getting an error saying that I am redefining highSoreDescriptor with a different type.) (结果我也收到一条错误消息,说我正在用另一种类型重新定义highSoreDescriptor。)

Also, is there a list somewhere of what selectors are available? 此外,是否有可用选择器的清单? I doubt that localizedCaseInsensitiveCompare: is what I want to use since the first property "_highScore" is an int and the second "_dateCreated" is a date. 我怀疑localizedCaseInsensitiveCompare:是我要使用的,因为第一个属性“ _highScore”是一个整数,第二个“ _dateCreated”是一个日期。 I read somewhere that the default is "compare" so can I just put "compare:"? 我在某处读到默认值是“比较”,所以我可以只输入“比较:”吗? (Found one answer, I think - I can use (intValue) for the first descriptor: (我认为找到了一个答案-我可以将(intValue)用于第一个描述符:

selector:@selector(intValue)] autorelease];

More reading makes me think that I can do away with the selector line entirely for the date sort. 更多的阅读使我认为我可以完全取消选择器行的日期排序。 Is that correct? 那是对的吗?

Lastly, if I say ascending:NO is that the same as descending? 最后,如果我说ascending:NO与降序相同? I would guess that it is, but one never knows with programming, does one? 我猜是这样的,但是对编程一无所知,对吗?

Do I wrap all of this code in its own method? 是否将所有这些代码包装在自己的方法中? Or can I (until later) just plunk it in the code where I am laying out the table? 还是可以(直到以后)将其插入我布置桌子的代码中?

This project is not ARC. 该项目不是ARC。

To answer my own question, with a little help from a friend, I was basically doing two things wrong. 要回答我自己的问题,在朋友的一点帮助下,我基本上做错了两件事。 First, I was writing the code outside of a method - which is why I was getting all of the errors about initializer elements. 首先,我在方法之外编写代码-这就是为什么我遇到有关初始化器元素的所有错误的原因。 I guess I was very tired when I was adding this. 我想我在添加时非常累。

As for the actual sorting, I deleted the selector option from the descriptor description and the sorting actually happened! 至于实际的排序,我从描述符描述中删除了选择器选项,排序实际上发生了!

Lastly, yes, ascending:NO is equal to descending. 最后,是的, ascending:NO等于降序。

The last bit, will have to wait until I a ready to tackle more refactoring of the application. 最后一点,必须等到我准备好应对应用程序的更多重构为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM