core-data

Sort Descriptors

Ordering Data Returned By Fetch Requests

Set the NSFetchRequest property sortDescriptors to determine how data is returned.

let fetchRequest = NSFetchRequest(entityName: "NAME_OF_ENTITY")
let sortDescriptor = NSSortDescriptor(key: "NAME_OF_ATTRIBUTE", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]

Multiple Sort Descriptors

You can also set multiple sort descriptors, to sort by one attribute within another. For example, return all entries ordered by date, and by name within each date:

let fetchRequest = NSFetchRequest(entityName: "NAME_OF_ENTITY")
let sortDescriptor1 = NSSortDescriptor(key: "name", ascending: true)
let sortDescriptor2 = NSSortDescriptor(key: "date", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2]

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow