How to ignore prefixes "A" and "The" when sorting using NSFetchedResultsController?

I use CoreData to store book information stored in an SQLite database. When sorting by title, I would like to ignore the prefixes "A" and "The". For example, when you have books: Codebook and Farm riddle, I want Fermat Enigma to appear before Codebook because The should be ignored.

I could not find any way to do this, except to add a persistent property that would contain a header without prefixes. (In the example above, this property says that "titleNoPrefixes" should contain the string "Codebook"). Sorting based on calculated properties (such as transient properties) is not possible in CoreData.

Adding a persistent property is obviously a bad database design, but I don't see another way. Any suggestions?

+4
source share
2 answers

Joshua

Your problem is almost not related to Core Data as a persistent repository, this is a general sorting of the database by text problem. The traditional solution to your problem is to have a normal form field that is used as a sort category. You may want to omit anything you want and add your own buzz words for this field. Since these are only names, it is not particularly expensive, reasonable. It is also an insert time operation, not an op search time. Finally, transition variables exist only in RAM. I believe these are NULL values ​​in the data store. Therefore, a search in them is meaningless and raises an exception.

Andrew

+1
source

You can write your own NSString category that implements:

  • Creating a copy of NSString with deleted articles (assuming you want to keep the original string)
  • Compare strings returned by C # 1.
+1
source

Source: https://habr.com/ru/post/1392138/


All Articles