ios - Sort Core Data attributes both by Date and Character -


i trying search core data, default searches title attribute need sort results date well.

my code searches title attribute how can make 2 predicates , connect them context ?

func textfield(textfield: uitextfield, shouldchangecharactersinrange range: nsrange, replacementstring string: string) -> bool {      let currenttext = textfield.text ?? ""     let prospectivetext = (currenttext nsstring).stringbyreplacingcharactersinrange(range, withstring: string)      //load data core data     let appdel : appdelegate = uiapplication.sharedapplication().delegate as! appdelegate     context =  appdel.managedobjectcontext      {          request =  nsfetchrequest(entityname: "event")         let sort:nssortdescriptor = nssortdescriptor(key:"title", ascending: true)         request.sortdescriptors = [sort]         let searchpredicate = nspredicate(format: "title contains[c] %@", prospectivetext)         request.predicate = searchpredicate         results = try context.executefetchrequest(request)          animatetablecell()       } catch {          print("error")     }      return true; } 

it's not 100% clear whether you're interesting in selection or sorting. predicates used determine records select, , sort descriptors used sort results.

for sorting, request.sortdescriptors array of descriptors. can have multiple descriptors, example:

let sort:nssortdescriptor = nssortdescriptor(key:"title", ascending: true) let sort2:nssortdescriptor = nssortdescriptor(key:"date", ascending: false) request.sortdescriptors = [sort, sort2] 

should sort ascending title, descending date (i.e. items same title should in descending order date).


Comments