summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tags.clj6
-rwxr-xr-xsrc/utils.clj17
2 files changed, 13 insertions, 10 deletions
diff --git a/src/tags.clj b/src/tags.clj
index fb530ea..76bc762 100644
--- a/src/tags.clj
+++ b/src/tags.clj
@@ -336,10 +336,8 @@ WHERE EXISTS
(let [ids (map maybe-parse-int ids)
query (fetch-dumps-by-message-id-query :num-messages (count ids))
raw-rows (do-select (vec (concat [query] ids)))
- tagged-rows (map parse-tags-from-row-as-tag-map raw-rows)
- index-func (fn [row]
- (index-of #(= (:message_id row) %) ids))]
- (for [m (sort-by index-func tagged-rows)]
+ tagged-rows (map parse-tags-from-row-as-tag-map raw-rows)]
+ (for [m (sort-by-index-in tagged-rows ids :message_id)]
(let [favers (get (:tags m) "favorite")
favorited (and viewer-nick
(boolean (some #(= % viewer-nick) favers)))
diff --git a/src/utils.clj b/src/utils.clj
index 25814f9..84f185a 100755
--- a/src/utils.clj
+++ b/src/utils.clj
@@ -446,10 +446,15 @@
{ :result result :time (System/currentTimeMillis)})
result)))))
-;; From Programming Clojure by Stuart Halloway
+;; Misc index/sorting funcs
-(defn index-filter [pred coll]
- (for [[idx elt] (indexed coll) :when (pred elt)] idx))
-
-(defn index-of [pred coll]
- (first (index-filter pred coll)))
+(defn sort-by-index-in
+ ([c1 c2] (sort-by-index-in c1 c2 nil nil))
+ ([c1 c2 f1] (sort-by-index-in c1 c2 f1 nil))
+ ([c1 c2 f1 f2]
+ (let [c2-map (zipmap (if f2 (map f2 c2) c2)
+ (range (count c2)))
+ sort-func (if f1
+ #(get c2-map (f1 %))
+ #(get c2-map %))]
+ (sort-by sort-func c1))))