更具体地说:我如何按某个键的值对结构向量进行排序?
例如,如果我有:
(defstruct Item :weight :value :cost
(def my-items [(struct Item 30 50 5)
(struct Item 15 75 20)
(struct Item 50 10 35)])
Run Code Online (Sandbox Code Playgroud)
我怎样才能对矢量中的所有项目进行排序,比方说,值?
使用排序方式
Clojure 1.4.0
user=> (defstruct Item :weight :value :cost)
#'user/Item
user=> (def my-items [(struct Item 30 50 5)
(struct Item 15 75 20)
(struct Item 50 10 35)])
#'user/my-items
user=> (sort-by :value my-items)
({:weight 50, :value 10, :cost 35} {:weight 30, :value 50, :cost 5} {:weight 15, :value 75, :cost 20})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
121 次 |
| 最近记录: |