     (let [[query & params] results-query
           limited-query (add-limit-clause limit query)
           query-results (limited-query-to-vec limit (apply vector limited-query params))
-          results       {:result (post-process-results query-results paging-options)}]
+          results       {:result
+                          (post-process-results
+                            query-results
+                            (:order-by paging-options))}]
       (if count-query
         (assoc results :count (get-result-count count-query))
         results))))

                  (into {} (for [param params :when (:name param)]
                             [(: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)]

        (comp-fn2 x y)
        val1))))

(defn order-by-expr?
  "Predicate that returns true if the argument is a valid expression for use
  with the `order-by` function; in other words, returns true if the argument
  is a 2-item vector whose first element is an `ifn` and whose second element
  is either `:ascending` or `:descending`."
  [x]
  (and
    (vector? x)
    (ifn? (first x))
    (contains? #{:ascending :descending} (second x))))

(defn order-by
  "Sorts a collection based on a sequence of 'order by' expressions.  Each expression
  is a tuple containing a fn followed by either `:ascending` or `:descending`;
  returns a collection that is sorted based on the values of the 'order by' fns
  being applied to the elements in the original collection.  If multiple 'order by'
  expressions are passed in, their precedence is determined by their order in
  the argument list."
  [order-bys coll]
  {:pre [(sequential? order-bys)
         (every? order-by-expr? order-bys)
         (coll? coll)]}
  (let [comp-fns    (map (fn [[f order]] (ordered-comparator f order)) order-bys)
        final-comp  (reduce compose-comparators comp-fns)]
    (sort final-comp coll)))

(defn- order-by->tuple
  "Convert an order-by entry from the paging middleware into
  a tuple valid for use by the `ordered-comparator` fn."
  ;; TODO: we should handle the conversion from asc/desc strings
  ;;  to the :ascending :descending keywords in the paging
  ;;  middleware, which should eliminate most or all of the
  ;;  need for this function
  [{:keys [field order]}]
  {:pre [(string? field)
         ((some-fn nil? string?) order)]}
  [(keyword field)
   (if (or (nil? order) (= "asc" (string/lower-case order)))
      :ascending
      :descending)])

(defn- post-process-results
  "Given the results of the query and the optional order-by paging clauses,
  consolidate the results into a form appropriate for returning to the user
  and sort them based on the order-by clauses, if there are any."
  [query-results order-bys]
  {:pre  [(vector? query-results)
          ((some-fn nil? vector?) order-bys)
          (every? map? order-bys)]
   :post [(vector? %)]}
  (let [consolidated-results (consolidate-resource-params query-results)]
    (if (empty? order-bys)
      consolidated-results
      (let [order-bys (map order-by->tuple order-bys)]
        (vec (order-by order-bys consolidated-results))))))

(defn limited-query-resources
  "Take a limit, a map of paging options, and a map of SQL queries as
