I'm fetching objects from a Core Data database for an iOS app that have multiple properties, and on my 3rd of the sort keys, I want to sort on Job Title, alphabetically ascending, but with the twist that "Chair" (i.e. the position of Department Chairman) sorts ahead of all other job titles (even those that are alphabetically ahead of it). The code I have below attempts to use the third sort descriptor to sort "Chair" above all other faculty positions using a Boolean, and then the 4th sort descriptor ranks all positions (i.e. sorting among all the job titles besides "Chair") alphabetically.
As is, I get an error on that third descriptor, having tried various permutations of the syntax that I can think of.
If I comment out that third descriptor, everything works & I get good results, other than the job title of Chairman falls in alphabetical order (so "Adjunct" then "Chair" then "Emeritus" and etc). What I want is -- after the first and second sorting keys have been applied -- for job titles to sort "Chair" first and then "Ajunct", "Emeritus", etc.
How should I be coding that third sort descriptor to make that happen?
I suspect that this question or this question may accomplish this, but the answers to both of those are in Objective C, and I'm not able to follow that enough to see how I'd convert either of those solutions to Swift.
func allProjects() -> [Projects] {
// fetches the array of Projects from Core Data & returns it.
do {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Projects")
// Order that I want to sort:
// 1 First by Month/Year (expressed as an Int32, 201803 = March 2018, for instance
// 2 then by Department (Astrology then Biology then Chemistry then Economics then etc)
// 3 then by departmentPosition = “Chair” — put the Chairman’s project(s) first
// 4 then by departmentPosition — everybody ELSE in alpha order: Adjunct > Emeritus > Lecturer > etc
// 5 then by Industry Partner in alphabetical order
let sortDescript1 = NSSortDescriptor(key: "yearMonthInt", ascending: false)
let sortDescript2 = NSSortDescriptor(key: "department", ascending: true)
let sortDescript3 = NSSortDescriptor(key: "{departmentPosition == ‘Chair’}", ascending: false). // Problem is here
let sortDescript4 = NSSortDescriptor(key: "departmentPosition", ascending: true)
let sortDescript5 = NSSortDescriptor(key: "industryPartner", ascending: true)
fetchRequest.sortDescriptors = [sortDescript1, sortDescript2, sortDescript3, sortDescript4, sortDescript5]
do {
let fetched = try myContext.fetch(fetchRequest) as! [Projects]
return fetched
}
} catch {
fatalError("Couldn't fetch [Projects]: \(error)")
}
}
EDIT 1: An idea that doesn't quite work
It was suggested in a comment that the comparison needed to be
let sortDescript3 = NSSortDescriptor(key: "departmentPosition", ascending: true, comparator: myComparator)
The problem is, that also generates an error (CoreData: error: exception handling request: <NSSQLFetchRequestContext: 0x7b3400037e90> , unsupported NSSortDescriptor (comparator blocks are not supported)
), and searching for the text of that error turns up this question/answer, which states that you can't use comparator blocks with Core Data.