             contentStr.append("\(sp)\"type\": \"\(modelRef.type)\",\(nl)")
             if modelRef.required.count > 0 {
                 let encoder = JSONEncoder()
+                if #available(OSX 10.13, *) {
+                    encoder.outputFormatting = .sortedKeys
+                } else {
+                    // Fallback on earlier versions
+                }
                 do {
-                    let encodedData = try encoder.encode(modelRef.required)
+                    let encodedData = try encoder.encode(modelRef.required.sorted())
                     if let json = String(data: encodedData, encoding: .utf8) {
                         contentStr.append("\(sp)\"required\": \(json),\(nl)")
                     } else {

        let nl = pretty ? "\n" : ""

        let encoder = JSONEncoder()
        var fieldCount = 1
        for (field, fieldProps) in properties {
            propertyStr.append("\(sp)\"\(field)\": ")

        definitionsStr = ",\(nl)"
        definitionsStr.append("\(sp)\"definitions\": {\(nl)")
        var modelCount = 0
        for model in self.definitions.keys.sorted() {
            modelCount += 1
            let encodedModel = try JSONEncodeModel(model: model, pretty: pretty, depth: depth + 1)
            definitionsStr.append(encodedModel)
