Using NSSortDescriptor to sort build numbers with Core Data

回眸只為那壹抹淺笑 提交于 2019-12-12 17:29:59

问题


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

Each line represents an NSString.

How would I do this properly or is there another way I should try?


回答1:


A sort descriptor using localizedStandardCompare: should produce the intended result:

[NSSortDescriptor sortDescriptorWithKey:@"buildNumber"
                              ascending:YES
                               selector:@selector(localizedStandardCompare:)];

localizedStandardCompare: is the "Finder-like" comparison and strings containing numbers are sorted according to their numerical value.

In Swift:

NSSortDescriptor(key: "buildNumber", ascending: true,
            selector: #selector(NSString.localizedStandardCompare))


来源:https://stackoverflow.com/questions/17847240/using-nssortdescriptor-to-sort-build-numbers-with-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!