简体   繁体   English

如何对 clojure 中的嵌套序列进行排序?

[英]How do I sort a nested sequence in clojure?

I have a nested map in which the keys are numbers as follows:我有一个嵌套的 map ,其中的键是如下数字:

{:18 [Lan Yuhong 165 Happy Lane 345-4533], :33 [Man Yuhong 165 Happy Lane 345-4533], :3 [Tan Yuhong 165 Happy Lane 345-4533], :6 [Jan Yuhong 165 Happy Lane 345-4533], :5 [Han Yuhong 165 Happy Lane 345-4533], :7 [Fan Yuhong 165 Happy Lane 345-4533], :1 [Sue Jones 43 Rose Court Street 345-7867], :9 [John Smith 123 Here Street 456-4567]}

which I want to sort on the basis of the first entry (ie, keys) as follows:我想根据第一个条目(即键)进行排序,如下所示:

{:1 [Sue Jones 43 Rose Court Street 345-7867], :3 [Tan Yuhong 165 Happy Lane 345-4533], :5 [Han Yuhong 165 Happy Lane 345-4533], :6 [Jan Yuhong 165 Happy Lane 345-4533], :7 [Fan Yuhong 165 Happy Lane 345-4533], :9 [John Smith 123 Here Street 456-4567], :18 [Lan Yuhong 165 Happy Lane 345-4533], :33 [Man Yuhong 165 Happy Lane 345-4533]}

However, the first entry in each list is treated as a string instead of an integer and the sort/sort-by return a lexographically sorted list as follows.但是,每个列表中的第一个条目被视为字符串而不是 integer,并且sort/sort-by返回按字典顺序排序的列表,如下所示。 Please note that the mps is stored in the new-map variable in my program.请注意,mps 存储在我程序的new-map变量中。

(do
    (into (sorted-map) new-map)
)

The output is: output 是:

([1 Sue Jones 43 Rose Court Street 345-7867] [18 Lan Yuhong 165 Happy Lane 345-4533] [3 Tan Yuhong 165 Happy Lane 345-4533] [33 Man Yuhong 165 Happy Lane 345-4533] [5 Han Yuhong 165 Happy Lane 345-4533] [6 Jan Yuhong 165 Happy Lane 345-4533] [7 Fan Yuhong 165 Happy Lane 345-4533] [9 John Smith 123 Here Street 456-4567])

How can I do this without using any loops?我怎样才能在不使用任何循环的情况下做到这一点? I am trying to avoid the use of loops here.我试图避免在这里使用循环。 However, a recursive function would be okay.但是,递归 function 可以。

You almost had it.你几乎拥有它。 Just add:只需添加:

(sort-by #(Integer/parseInt (first %)) new-list)

full code:完整代码:

(ns tst.demo.core
  (:use tupelo.core tupelo.test))

(dotest
  (let [data [["18" "Lan Yuhong" "165" "Happy Lane" "345-4533"]
              ["33" "Man Yuhong" "165" "Happy Lane" "345-4533"]
              ["3" "Tan Yuhong" "165" "Happy Lane" "345-4533"]
              ["6" "Jan Yuhong" "165" "Happy Lane" "345-4533"]
              ["5" "Han Yuhong" "165" "Happy Lane" "345-4533"]
              ["7" "Fan Yuhong" "165" "Happy Lane" "345-4533"]
              ["1" "Sue Jones" "43" "Rose Court Street" "345-7867"]
              ["9" "John Smith" "123 Here Street" "456-4567"]]]
    (spyx-pretty (sort-by #(Integer/parseInt (first %)) data))))

with result结果

(["1" "Sue Jones" "43" "Rose Court Street" "345-7867"]
 ["3" "Tan Yuhong" "165" "Happy Lane" "345-4533"]
 ["5" "Han Yuhong" "165" "Happy Lane" "345-4533"]
 ["6" "Jan Yuhong" "165" "Happy Lane" "345-4533"]
 ["7" "Fan Yuhong" "165" "Happy Lane" "345-4533"]
 ["9" "John Smith" "123 Here Street" "456-4567"]
 ["18" "Lan Yuhong" "165" "Happy Lane" "345-4533"]
 ["33" "Man Yuhong" "165" "Happy Lane" "345-4533"])

Update更新

For your modified question, see the function group-by对于您修改后的问题,请参阅 function group-by

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM