[英]How can I sort an NSTableColumn of NSStrings ignoring “The ” and “A ”?
I've got a simple Core Data application that I'm working on to display my movie collection. 我有一个正在使用的简单Core Data应用程序,用于显示我的电影收藏。 I'm using an
NSTableView
, with it's columns bound to attributes of my Core Data store through an NSArrayController
object. 我正在使用
NSTableView
,它的列通过NSArrayController
对象绑定到我的Core Data存储的属性。 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(ie Z before a). 我遇到的问题是字符串排序,它们进行排序,但是它是以标准字符串方式完成的,大写字母在小写字母之前(即Z在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.
除了使大小写排序正常工作外,我还希望在对字符串进行排序时能够忽略前缀“ The”或“ A”。
What is the best way to go about this in Objective-C/Cocoa? 在Objective-C / Cocoa中解决此问题的最佳方法是什么?
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. 在Interface Builder中,将每个表列的“
Sort Key
字段设置为与要应用于托管对象的sortDescriptor相对应。
For each column that you want to ignore "A" and "the", you'll have to create an extra method on your managed objects. 对于您要忽略“ A”和“ the”的每一列,您必须在托管对象上创建一个额外的方法。 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". 如果您要排序的属性是
title
,则创建一个名为cleanedTitle
的新方法,该方法主要使用[self title]
并cleanedTitle
“ a”和“ the”。 Then in Interface Builder, tell your table column to use the cleanedTitle
sort key, instead of the title
sort key. 然后在Interface Builder中,告诉您的表列使用
cleanedTitle
排序键,而不是title
排序键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.