使用NSSortDescriptor对带有核心数据的内部版本号进行排序 [英] Using NSSortDescriptor to sort build numbers with Core Data
问题描述
我正在尝试将 NSSortDescriptor
与Core Data一起使用来对内部版本号/软件版本(NSStrings)进行排序.内部版本号实际上只是NSStrings,但这似乎很难.我猜是因为有多个不规则的小数点.
I'm trying to use NSSortDescriptor
with Core Data to sort build numbers/software versions (NSStrings). The build numbers are actually just NSStrings but it seems to have a hard time with this. I'm guessing because of the multiple and irregular decimal points.
例如,我想对以下内容进行排序.
For example, I would like to sort the following.
1.7.6
1.8
1.6.2
1.9.0.1
2.0
每行代表一个 NSString
.
我将如何正确执行此操作,或者还有其他尝试方法?
How would I do this properly or is there another way I should try?
推荐答案
使用 localizedStandardCompare:
的排序描述符应产生预期的结果:
A sort descriptor using localizedStandardCompare:
should produce the intended result:
[NSSortDescriptor sortDescriptorWithKey:@"buildNumber"
ascending:YES
selector:@selector(localizedStandardCompare:)];
localizedStandardCompare:
是类似Finder"的比较,包含数字的字符串根据它们的数值排序.
localizedStandardCompare:
is the "Finder-like" comparison and strings containing numbers
are sorted according to their numerical value.
在 Swift中:
NSSortDescriptor(key: "buildNumber", ascending: true,
selector: #selector(NSString.localizedStandardCompare))
这篇关于使用NSSortDescriptor对带有核心数据的内部版本号进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!