Q:

Sorting fetch results by Bool attribute in CoreData Swift 4

I am creating a ToDo list app and would like to sort the tableviewcontroller by priority.  if the task is important i would like it on top of the todo list, if it is not important than i would like it under/after the important tasks.  I have been trying to figure out how to sort this but have not been able to.  Appreciate some help.  
class ToDoTableViewController: UITableViewController {

var toDos : [ToDoCoreData] = []

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewWillAppear(_ animated: Bool) {
    getToDos()
}

func getToDos() {
    if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext{

        if let coreDataToDos = try? context.fetch(ToDoCoreData.fetchRequest()) as? [ToDoCoreData] {
            if let theToDos = coreDataToDos {
                toDos = theToDos
                tableView.reloadData()
            }
        }
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

    let toDo = toDos[indexPath.row]

    if let name = toDo.name {

        if toDo.important {
            cell.textLabel?.text = "❗️" + name
        } else {
            cell.textLabel?.text = toDo.name
        }
    }
    return cell
}

A:

Update getToDos() function like that:
func getToDos() {
    if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext{

        let fetchRequest: NSFetchRequest<ToDoCoreData> = ToDoCoreData.fetchRequest()
        let descriptors = [NSSortDescriptor(key: "important", ascending: true)] // try true/false

        fetchRequest.sortDescriptors = descriptors

        if let coreDataToDos = try? context.fetch(fetchRequest) {
            if let theToDos = coreDataToDos {
                toDos = theToDos
                tableView.reloadData()
            }
        }
    }
}

