How can I sort an NSTableColumn of NSStrings ignoring “The ” and “A ”?

喜你入骨 提交于 2019-12-08 06:32:07

问题


I've got a simple Core Data application that I'm working on to display my movie collection. I'm using an NSTableView, with it's columns bound to attributes of my Core Data store through an NSArrayController object. At this point the columns sort fine(for numeric values) when the column headers are clicked on.

The issue I'm having is with the String sorting, they sort, however it's done in standard string fashion, with Uppercase letters preceding lowercase(i.e. Z before a). In addition to getting the case sorting to work properly, I would like to be able to ignore a prefix of "The " or "A " when sorting the strings.

What is the best way to go about this in Objective-C/Cocoa?


回答1:


In Interface Builder, you set the Sort Key field of each table column to correspond to which sortDescriptor you want applied to your managed objects.

For each column that you want to ignore "A" and "the", you'll have to create an extra method on your managed objects. If the property you're sorting is title, then create a new method called cleanedTitle that basically takes [self title] and strips out "a" and "the". Then in Interface Builder, tell your table column to use the cleanedTitle sort key, instead of the title sort key.



来源:https://stackoverflow.com/questions/2796560/how-can-i-sort-an-nstablecolumn-of-nsstrings-ignoring-the-and-a

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