         val1))))
 
 (defn- sort-on-order-by
-  "TODO docs & conditions"
+  "Sort the results of the query based on the order by clauses."
   [query-results order-bys]
+  {:pre  [(vector? query-results)
+          (vector? order-bys)
+          (every? map? order-bys)]
+   :post [(vector? %)
+          (= (count %)
+             (count query-results))]}
   (let [order-bys  (mapv #(update-in % [:field] keyword) order-bys)
         comp-fns   (map build-comp-fn order-bys)
         final-comp (reduce combine-comp-fns comp-fns)

(def v3-query->sql
  (partial query->sql resource-operators-v3))

(defn- aggregate-resource-parameters
  "TODO docs & conditions"
  [query-results]
  (let [metadata_cols [:certname :resource :type :title :tags :exported :file :line]
        metadata      (apply juxt metadata_cols)

                             [(:name param) (json/parse-string (:value param))])))))))

(defn- build-comp-fn
  "Given a resource field and order direction, return a comparator
  function that takes two resources and sorts them in ascending or
  descending order based on the values of the provided field."
  [{:keys [field order]}]
  {:pre  [(keyword? field)
          (or (= order "ASC")
              (= order "DESC")
              (nil? order))]
   :post [(fn? %)]}
  (fn [x y]
    (if (or (= order "ASC") (nil? order))
      (compare (x field) (y field))
      (compare (y field) (x field)))))

(defn- combine-comp-fns
  "Given two comparator functions, return a comparator function that
  delegates to the first comparator, and if the result is equal, then
  delegates to the second comparator."
  [comp-fn1 comp-fn2]
  {:pre  [(fn? comp-fn1)
          (fn? comp-fn2)]
   :post [(fn? %)]}
  (fn [x y]
    (let [val1 (comp-fn1 x y)]
      (if (= val1 0)
