Clojure使用键名作为决胜局按值排序哈希映射

时间:2016-12-04 17:51:36

标签: clojure

我希望能够根据键的值对哈希映射进行排序,在出现平局时使用键名本身

我有一个来自Clojure wiki https://clojuredocs.org/clojure.core/sorted-map-by#example-542692d5c026201cdc327094

的排序函数
getLengthAtPoint = function(x, y, curve){
  var len = curve.getTotalLength(),
    tol = len/100,
    inc = len/100,
    err,
    errMin = 99999, // infinity
    cond = true,
    lengthIter = 0,
    lengthMin = 0;

  do {
    err = dist(curve.getPointAtLength(lengthIter), {x, y}); //pythagore
    cond = err>tol;
    if (err < errMin){
      errMin = err;
      lengthMin = lengthIter;
    }
    lengthIter = lengthIter + inc;
    if (lengthIter > len) {cond = false;}
  } while (cond);

  return (lengthMin/len);
}

目前我能够

(defn order-map [target]
  (into (sorted-map-by (fn [key1 key2]
                         (compare [(target key2) key2]
                                  [(target key1) key1]))) target))

输出

(-> "notarealroom" frequencies order-map)

但是我希望能够使用相同的值对这些键进行排序,例如按字母顺序{\o 3, \r 2, \a 2, \t 1, \n 1, \m 1, \l 1, \e 1} \r给出类似以下内容的内容......

\a

我不确定如何修改比较功能来处理这种抢七场景

1 个答案:

答案 0 :(得分:2)

你快到了。以下应该做正确的事情:

(defn order-map [target]
  (into (sorted-map-by (fn [key1 key2]
                         (compare [(target key2) key1]
                                  [(target key1) key2]))) target))