CBLView Multiple Field Sort?

I have a document that has a “startBy” (iso date string) and a “name” (string) field in its row value. I need to sort on startBy, and then name secondarily. I set an array of sortDescriptors for both but the query only sorts based on the first descriptor in the array. Is there a reason why it’s only sorting based on the first descriptor and not both?

        let view = database.viewNamed("Inspections")
        view.setMapBlock({ (doc, emit) in
            guard let type = doc["type"] as? String, type == "Inspection" else {
                return
            }
            
            
            // Nested JSON object that has "startBy" (date string) and "name" (string)
            let value = doc["inspection"] as! [String: Any]
            
            // Make sure its not yet started.
            guard value["startedAt"] == nil else {
                return
            }
            
            // Using "startBy" date string value as key also works to sort by that
            let key = value["startBy"]!
            
            emit(key, value)
        }, version: "1")
        
        let query = view.createQuery()
        
        let startBy = NSSortDescriptor(key: "value.startBy", ascending: true)
        let name = NSSortDescriptor(key: "value.name", ascending: true)
        query.sortDescriptors = [startBy, name]

It should be sorting on both. Do the values returned by the query actually have a name property?

If you want to do this more efficiently, just emit [value["startBy"], value["name"]] as the key, and CBL will sort the way you want without having to postprocess with sort-descriptors.

Another thing that would cause what you’re seeing is if the startBy values are all different, for example if they’re full date-time strings with precision down to the second.